Android Developer Reference

This is the official Google Android site for developers. Here you will find all the documentation you need to develop rich android applications.

You will most likely visit this site often.

All tutorials and sample code are credited to their respected owners.

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

ListView Widget

In this topic we will be covering ListViews and their associated Adapters.

This topic will talk about creating a listview and how to catch events when the user selects one of the items.

A screenshot of the first example is shown here:

In this example we will cover these steps to give us the results show here.
  1. Create a ListView widget in our main.xml file.
  2. Create a string array data to load into our ListView.
  3. Create a row layout definition to be used for the ListView row items.
  4. Create an instance variable, and an ArrayAdapter.
  5. Set the adapter to use for the ListView to display the data.
ListViews work by assigning data values to an adapter, the adapter then interprets the data, then the adapter will fill in the view that was passed in to display the data.

Now let's go through the steps required for this example.

Open up your main layout XML file and add this bit of code below.

You can see here we added a <ListView> element and gave it an id of @+id/listView. We also set the layout width and height to fill the screen.

The next step is to add our list items, in this example we will use a string array to populate our ListView. Open up your strings.xml resource file and add the code below.

This creates a string array called list_items that we can use to fill our ArrayAdapter.

Next we need to create another layout XML file to represent our ListView row item. We will show you the code then it will be easier explain it.

The code above creates a TextView widget to be used to display the individual item data in the ListView.

Now that we have our code written, let's hook it all up in our activity. Firstly, open up your main activity and add an instance variable to hold our ListView shown below.

Now, enter this code in the onCreate() method of your activity.

Let's examine this code a bit.

The first statement simple initializes the listView variable using the findViewById() method. Then a String[] array variable is created to contain the string array we created earlier, this is done by using the getResources().getStringArray() method. Then the meat and potatoes of populating the ListView is done by assigning the ArrayAdapter to the ListView using the setAdapter() method of the ListView.

You can change the appearance of the ListView items by modifying the underlying row layout XML file. We will demonstrate this next.

Customizing the appearance of the ListView

Next we will look at how we can change the appearance of our ListView items.

As mentioned previously, we can accomplish this by modifying the underlying item's layout XML file, in our case that would be the list_item.xml file.

Let's start by changing the lines android:textSize="16dip" to "18dip" and add android:textColor="#A52A2A". The new ListView is shown below.

Now run the app again and it should look like this

NOTE: Depending on your resolution and android version the appearance may differ than what you see here.

Populate a ListView from a Database

In this section we will learn how to populate a ListView from a database column. This example assumes you already have a working database. Even though we will be using arbitrary data here, it should be easy for you to modify it to hold your own data.

To make this happen, we get our database column data using the CursorAdapter class, then when can assign this adapter to our ListView.

The code to populate our ListView is shown below, check it out and then we'll go over it in more detail.

The first line of the method above calls the user defined method db.fetchAllTitles() which fills our cursor with the title field of our database. Then we initialize our String[] "columns" structure with the desired field name from the database. We now create an int[] array to hold our view id's in which we will place the column data. Now we can pass our "columns" and "to" values to our SimpleCursorAdapter class. And finally assign the adapter to our ListView.

You can add more things like a stored image from the database by adding the column and view id of that database field and add these values to our "columns" and "to" variables respectively.

We will cover Databases, and multiple fields, and it's relative methods and stuff in our Database tutorials.

Some Commonly used Methods and Attributes

As mentioned on the previous page we can modify a lot of the ListView attributes by modifying the underlying item's layout XML file, in our case that would be the list_item.xml file.

Here is a list of some common attributes and methods of the ListView



Events and Callbacks