Binding Android ListView with String Array using ArrayAdapter

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.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
      android:layout_width="fill_parent" />


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 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.

public class MainActivity extends Activity {

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

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

 Android ListView ArrayAdapter.png



About author

User avatar

Waqas Anwar

Founder of, Senior Consultant, Microsoft .NET and Web Technologies