List View using Array Adapters – Part I

This entry is part 31 of 50 in the series Android Development Primer

Often times in your application when you want to list down a bunch of data you’re going to use a List View. It provides a predefined layout that helps create a list from a specific set of data. Now, inserting a list view in you activity’s layout is but trivial. Here we are going to see how to populate the list view.

It is to be remembered that a List can be implemented inside an activity in a number of ways. I will try and discuss all such ways in separate posts. Also remember that the classes we are going to use have overloaded constructors and we will be using any one of them. However, all of them are equally important in various situations, so I suggest you use the eclipse code-hinting feature to have a look at all those. Complete Source Code is at the bottom.

  • Start off an activity. Switch to the XML layout file and insert a ListView from the palette on the left.
  • Spread it all the way across the length and breadth of the activity.
  • Switch over to the file and declare the ListView. 
  • Create a string array containing elements that you want in the list. Try and put enough values so that you can also experience the scrolling feature of ListView provided by default in the ListView element.
  • We are now going to populate the ListView with elements from the string array with the help of an Array Adapter.
  • In order to declare the ArrayAdapter write the below lines:
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,,values);
  • We can also declare the ArrayAdapter using the below declaration:
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
  • As I said earlier, we can use one of many constructors of the ArrayAdapter class depending on our needs and the situation
  • values is the name of the string array containing the elements to be displayed.
  • Now, we have the ArrayAdapter ready and in order to populate the list we need to assign this adapter to the ListView:
  • lv is the name of the ListView of my activity.
  • Save your work and run it on an emulator or device.

Array_Adapter1  Array_Adapter2

Understanding the Code:

  • Adapters may be seen as the bridge between the UI components and the data source that fill the data into the UI.
  • There are various types of Adapters – ArrayAdapters, SimpleCursorAdapters, SimpleAdapters etc. We can even make custom adapters that serves our purposes.
  • The declarations that I have shown here has the below constructor:
    ArrayAdapter = new ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
  • Once we have setup the bridge i.e. ArrayAdapter we need to use it to populate the List. We do this by using the setAdapter() method.



<RelativeLayout xmlns:android=""
    android:paddingTop="@dimen/activity_vertical_margin" >

        android:layout_alignParentTop="true" >

package com.nero.myfirstapp;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        String values[]=new String[]{"Vergil", "Dante", "Sparda", "Nero", "Arkham", "Agni", "Rudra", "Beowulf", "Nevan"};
        ListView lv = (ListView) findViewById(;
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,,values);
Series Navigation<< Content Providers in AndroidList View using Array Adapters – Part II : Using ListActivity >>

Leave a Reply

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


* Copy This Password *

* Type Or Paste Password Here *

68,488 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>