Android Application Development – ListView Tutorial

There are different ways to implement a list of multiple line items on Android Apps. They can be used for data selection as well as drilldown navigation. ListView is a group that can display our scrollable items. Usually when the user click on a item, some action can be performed.
Our ListDemo will extend ListActivity and use an Android build-in list view, simple_list_item_1 layout. We will not care much in this part for performance or manage huge data, this will be covered on future post, so make sure you subscribe to our feeds. Our example will be an array with some data and make use of ArrayAdapeter(...).

Create a new Class ListDemo

After we have created our ListDemo, we have to extend the ListActivity class. After this the next step is creating a simply array with strings as follows:

String [] codeNames = {"Cupcake",
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat"};

Overriding onCreate(…) method

Now we have to override the onCreate method and implement the setListAdapter method as follows:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codeNames));

Here we have set the list adapter. So we “adapt” this data to be displayed on some way, in our case in a list. We are using the Android build-in simple_list_item_1 layout and for the last parameter we are giving our codeNames strings.

Now run this App on your Android Virtual Device. If everything is compiling right you will have a list with Android Code Names. But if you click on one of this items, nothing happens. For this we have to implement the onListItemClick(...) method.

Android-List-View-Tutorial-screen-source-code

Overriding onListItemClick(…) method

In case we want to display some message or other information if a user click on this items we have to implement onListItemClick method. Here we have to make attention of the position., so when item is selected it will show the correct value.
For this ListDemo, lets display a Toast Message when an item is selected. To do this paste the following code:

Toast.makeText(this, "Codename selected: " + codeNames[position], Toast.LENGTH_SHORT).show();

Now run again and after the App is launched select an item from the list. You will notice a toast message with the correct selected Item.

ListDemo.java Source Code

package net.lirent.android.demoapp;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
* Created by http://www.Lirent.net on 08/01/14.
*/
public class ListDemo extends ListActivity {
    String [] codeNames = {"Cupcake",
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, codeNames));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "Codename selected: " + codeNames[position], Toast.LENGTH_SHORT).show();
    }
}
Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *