[Android] Using an ArrayAdapter to Control a ListView’s Data

Data inside a ListView supplied by a ListAdapter. The manipulation of data, such as adding and removing items, is done through the adapter. The adapter will automatically make the ListView update itself to correspond to the change. The source for the list is set with by calling setAdapter with the source adapter. In this tutorial, an ArrayAdapter is used with a ListView.

The code for the following example is downloaded from here. The example uses a ListActivity to provide a ListView:

    private ArrayAdapter<String> dataAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        dataAdapter = new ArrayAdapter<String>(this, R.layout.item,
            R.id.itemName);
        dataAdapter.add("apple");
        dataAdapter.add("orange");
        dataAdapter.add("tomato");

        setListAdapter(dataAdapter);
    }

A ListActivity is a form of Activity, but automatically provides a ListView. The method setListAdapter is provided by the ListActivity class and will set the adapter of the ListView. Running this code alone should produce a list looking like this:

The activity also provides an options menu that is accessible when the menu key is pressed. Accessible from the menu is the option to add and remove items from the list. Selecting the add options pops open the following dialog:

The code that actually adds the contents from the dialog to the ListView is inside the DialogInterface.OnClickListener associated with the add button:

    builder.setPositiveButton(R.string.addButtonLabel,
        new DialogInterface.OnClickListener()
    {

        public void onClick(DialogInterface dialog, int which)
        {
            Dialog source = (Dialog) dialog;
            EditText nameField = (EditText) source
                .findViewById(R.id.itemField);
            String name = nameField.getText().toString();

            EditText timesField = (EditText) source
                .findViewById(R.id.timesField);
            Integer times = Integer.valueOf(timesField.getText()
                .toString());

            if ((name.length() > 0) && (times > 0))
            {
                for (int count = 0; count < times; count++)
                {
                    dataAdapter.add(name);
                }
            }
            dialog.dismiss();
        }
    });

Lines 8 to 10 is just getting the string to add to the list. Lines 12 to 15 is getting the value in the times field. In Lines 17 to 23, if the item is given and the specified number of times is greater than zero, then the item is added however many times was specified. Line 21 is where the item is actually added to the list. Notice that the item is added to the adapter, not the view. This is ALL that is required to make the item to add to the list. Since back in onCreate, the adapter was already set as the ListView’s adapter, will automatically cause the ListView to update.

Selecting the remove option on the menu removes the last item from the list. As there is no dialog required, the code that does this is back in onOptionsItemSelected. More specifically, it is done by this part of the method:

    case REMOVE_ITEM:
        dataAdapter.remove(
            dataAdapter.getItem(
                dataAdapter.getCount() - 1));
        break;

Again, notice that the item is removed by calling remove on the adapter, not the ListView. The view is automatically updated when the item is removed. The files for this post can be downloaded here.

About these ads

6 Responses to [Android] Using an ArrayAdapter to Control a ListView’s Data

  1. jluclemo says:

    thanks a lot !

  2. Pingback: Filtering a ListView using an ArrayAdapter « Kah – The Developer

  3. Tim says:

    This really helped for what I was trying to accomplish…thanks

  4. ditsikts says:

    thanks. I understans listview and dialogs;)

  5. depot pulsa says:

    great thanks.you have just given me enlightenment

  6. Lion says:

    thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: