Android ListView ArrayAdapter, Manage Items, User interaction

This post describes how to use Android Listview ArrayAdapter. In the last post we talked about the ListView widget and how we need an adapter to show items inside this widget. We showed how an user can interact with this view using OnItemClicklistener. In this post we want to go further and show another type of adapter called ArrayAdapter. We will see, in the second part, how to handle list items at runtime adding or removing them.

Android ListView ArrayAdapter

In the last post we talked about the SimpleAdapter and how it is possible to add items to the ListView using this adapter. Another important and very useful adapter is the ArrayAdapter. This class is a standard Android class and it is very simple to use. We can use it every time we have an array or a list of items we want to show.

 Modifying  the source code of the last post, where we created a planet list we can replace the SimpleAdapter with Android ListView ArrayAdapter. In our case, the ArrayAdapter contains just strings to make things simpler. So we have:
[java]aAdpt=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,, planetsList);[/java]
where again android.R.layout.simple_list_item_1 is the layout for our single row and is the id of the TextView we want to fill with the planet name. In this case, the planetList is  simply a List of strings.
[java]List<String> planetsList = new ArrayList<String>();
private void initList() {
// We populate the planets


When you run the application, the result is the same shown in the last post. Now we want to go further and start analyizing how we can manage items dynamically.


Android ListView: Manage Items dynamically

Let’s suppose we want to manage the items shown inside the ListView dynamically, adding and removing items. First we focus our attention about the add functionality. To do so we need a button, so that when user presses it a dialog window appears. In the dialog window, we add an input text where the user can insert the planet name. To make the interface more friendly, we want the button is always visible even if when the list scrolls up and down. To do it we first create a LinearLayout like that:

[xml]<linearlayout android:layout_height=”match_parent”

<listview android:id=”@+id/listView”

<button android:id=”@+id/addBtn”
android:layout_width=”match_parent” android:onclick=”addPlanet”
android:text=”Add planet”>

Notice that we used a layout_weight attribute to give more “weight” to the ListView widget respect to the Button. If we run the code we have:

listview add button

In the Button, we added onClick attribute and we declared the method to be called when user clicks on it. If you want more information please look here.

So in this method, we simply open a dialog where user inserts the new planet name.

[java]// Handle user click

public void addPlanet(View view) {
final Dialog d = new Dialog(this);
d.setTitle(“Add planet”);
final EditText edit = (EditText) d.findViewById(;
Button b = (Button) d.findViewById(;
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String planetName = edit.getText().toString();
// We notify the data model is changed

So that when a user clicks on add button we have:

lis-view add item arrayadapter..and once the user clicks on add:

android listview add item on arrayadapter

The important thing to notice is the notifyDataSetChanged(). We call this adapter method on to notify that the items are changed and it is necessary to refresh the view.
As we did for the add functionality, we can implement the delete item. In this case, we want that when the user long-press on an item, it will be deleted. To handle this event, as we showed in the last post we overide the onContextItemSelected method in this way:
// This method is called when user selects an Item in the Context menu

public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
return true;

In this snippet, first we retrieve the AdapterContextMenynfo needed to get the item position and then we remove it from the list. Again we notify to the adapter that the list is changed.

    1. Ray October 27, 2013
    2. Natta January 28, 2014
    3. Lắm March 22, 2014
    4. shahnawaz June 9, 2016
    5. Pingback: Listview endless adapter in Android Tutorial December 13, 2016
    6. VIvek raj February 12, 2019
    7. Anonymous July 23, 2019

    Add Your Comment