Android: Create ListView in XML and populate items using ArrayAdapter

3 September 2012 By Nithya Vasudevan 48,811 views One Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

Project Description

  • In the previous Android ListView example, we created ListView in XML and populated its items using android:entries attribute on list view element which points to the <string-array> defined by external resource in res/values/strings.xml.
  • In this example, we will show how to create ListView in XML layout and populate its items from string array defined by external resource (res/values/strings.xml) programatically using ArrayAdapter.
  • Job of an Adapter
    • defines how each row is displayed (i.e) the layout for each row). It can be a user defined layout defined in a XML file placed in res/layout or it can be default ListView layouts provided by Android system. (We will show these two methods in this example).
    • provides data to each row in ListView.
  • Here, we are going to implement OnItemClickListener event listener which calls onItemClick() callback method where we retrieve and display the item selected by the user.

Environment Used

  • JDK 6 (Java SE 6)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • Android SDK 4.0.3 / 4.1 Jelly Bean
  • Android Development Tools (ADT) Plugin for Eclipse (ADT version 20.0.0)
  • Refer this link to setup the Android development environment


Create Android Project

  • Create a new Android Project and name it as “ListViewDemo“.
  • Enter the package name as ““.
  • Enter the Activity name as “ListViewActivity2“.
  • Click Finish.


Open res/values/string.xml and replace it with following content.

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, ListViewActivity2!</string>
    <string name="app_name">ListViewDemo</string>
     <string-array name="sports_array">
        <item>Shuttle Badminton</item>
        <item>Basket Ball</item>
        <item>Table Tennis</item>

The <string-array> element defines the list of strings that will be displayed as list item in the ListView layout.

XML layout file

Open main.xml file in res/layout and copy the following content.

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=””
    android:orientation=”vertical” >


In the XML layout file we have defined a ListView for displaying a scrollable list of items. These items can be of any type and for this example we have used string (TextView object) as each item.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Method 1: Using row layout defined by Android system

Activity class


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity2 extends Activity 
			implements OnItemClickListener {
    ListView listView;
    public void onCreate(Bundle savedInstanceState) {
        listView = (ListView) findViewById(;
	String[] sports = getResources().getStringArray(R.array.sports_array);
        //Row layout defined by Android: android.R.layout.simple_list_item_1
        listView.setAdapter(new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_1, sports)); 
    public void onItemClick(AdapterView<?> adapter, View view, 
				int position, long id) {
	Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
  • To load the list view items, we first get the string array resource and used it in ArrayAdapter

Method 2: Using own row layout defined in XML file

Create a new XML row layout file and name it as “list_item.xml” in res/layout folder and copy the following content.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
    android:padding="10dp" />

In the above Activity class, replace the listView.setAdapter() line with the following:

listView.setAdapter(new ArrayAdapter(this, R.layout.list_item, sports));


Run your application

Project Folder Structure

The complete folder structure of this example is shown below.

Tags: , , , , , , , , , , , , , , ,

  • Marian Zsemlye

    As always, not fully maid help.
    If you enable filterig in list you wont be able to get real data.

    - you have a list with 30 posieiton
    - you filter them, so you get the last as the firs (and only one)
    - after clicking on it you get value from FIRST one !!!