Tutorial / Android Development

Create Android ListView Using ListActivity Class

Introduction

Displaying info items in list format is a very common UI pattern in mobile application. Android provides a ListActivity class for Android developer to handle the ListView.

The ListActivity class is extends from Activity Class. The ListActivity has a default ListView layout and default onListItemClick() method for handling selection of list items.

In this tutorial, we are going to create an Android ListView using ListActivity Class.

*This article is assumes that you have already basic knowledge in Android development. If you haven’t already, please see Android Mobile Development to learn about basic Android development.

Creating Android Project

Create a new project by going to File -> New Android Project. Fill all the details and name your activity as FortuneCrunch.

Once the project is created, open the FortuneCrunch.java file and extends the FortuneCrunch class from ListActivity.

package com.yourname.tutorial.fortunecrunch;

import android.os.Bundle;

public class FortuneCrunch extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

You may encounter error when you extends the class from ListActivity. But no worry, the reason is we haven’t import the ListActivity class into our package yet, so let’s solve the problem by doing this: Ctrl+Shift+O. This will tells ECLIPSE to import necessary class / library into our package. This action will generate a new line of code in our FortuneCrunch class:

import android.app.ListActivity;

Setting-Up Information

Now, we need an array to store the list items’ label.

String[] apple_products = new String[] {"iPhone", "iPad", "iPad mini", "iPod"};

Providing Data To ListView Object

Adapter is used to provide data / information to ListView object. The following codes describes how can we use it to display the information that we defined in array:

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apple_products);
        setListAdapter(adapter);

An ArrayAdapter class can receives several paramters. The one we using here is receiving 3, which is – Context, Layout for the row and the Array of data.

This example uses a predefined layout(android.R.layout.simple_list_item_1) to display data in lists from Android System.

The setListAdapter() method has been used to assign the adapter to the ListView.

Import ArrayAdapter class into our package by doing Ctrl+Shift+O.

Now, run your project you can see a ListView with list of array items. :)

ListView with list of array items

Final Code

package com.yourname.tutorial.fortunecrunch;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;


public class FortuneCrunch extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        String[] apple_products = new String[] {"iPhone", "iPad", "iPad mini", "iPod"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apple_products);
        setListAdapter(adapter);

    }
    
}

  • Stumble it!
  • Share