Binding Android ListView with String Array using ArrayAdapter

Waqas Anwar
10 August 2011
63355 Views

This is my first tutorial on Android, so I have decided to start with a simple example. Android ListView control is one of the most popular controls available to Android developers, and It can be used in many different forms and can be customized to suit almost every possible application requirement. In this tutorial, I will demonstrate you how you can bind a simple array of Strings with ListView. In my future tutorials, I will show you more advance scenarios and layouts that can be achieved using ListView.

Before, I start, I am assuming that you have installed Eclipse, configured your Android development environment along with ADT, and you know how to create and run a simple Android project.

Create a new Android project in Eclipse with MainActivity as your default Activity and main.xml as your default layout file. Declare a ListView control in your main.xml layout file as shown in the following code.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
   <ListView
      android:id="@+id/listView1"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent" />


</LinearLayout>

The above code is using simple LinearLayout with vertical orientation, and a ListView is declared to cover the entire width and height of the parent container using the fill_parent as the value of both android:layout_height and android:layout:width properties. ListView also has a unique id listView1 that will be used in the MainActivity to reference the ListView control.

Following is the code you need in MainActivity.java. The code first access the ListView control by passing the generated id to findViewById function. Next the array of Strings is declared with some simple list of items in it. Next an instance of ArrayAdapter object is created by passing it the reference of current activity context using this. A reference of the Android predefined TextView resource id along with the array of objects as parameters in the constructor. In the end, adapter is provided to ListView using setAdapter function available in ListView class.


MainActivity.java
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ListView listView1 = (ListView) findViewById(R.id.listView1);
       
        String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
       
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);
       
        listView1.setAdapter(adapter);

    }
}

Run the project in Eclipse and you will see the output similar to the following screenshot in the Emulator.

 Android ListView ArrayAdapter.png