Android Tutorials
Working With Android Contacts
Accessing Data With Android Cursors
Creating Lists Using The Android ListActivity
Android 9 Patch Scaled PNG Image Guide
Working With Images In Android
Exploring Android LinearLayout And RelativeLayout
Writing A Basic Android Application
Installing The Android SDK In Eclipse

Creating Lists Using The Android ListActivity

Introduction

ListViews are used to create views with lists of data in Android applications. Lists can be populated with data from android data sources with cursors or with simple arrays. Custom views can be applied to Lists applying formatting including checkboxes and icons. Create new project called TestListActivities.

Basic ListActivity Usage

Layout

To create a visible list there are some specific elements that need to exist in the layout. The first is a ListView node with the android:id of "@android:id/list". This will be where the list is displayed in the view. The second one is a TextView with the android:id of "@android:id/empty". This second one will be displayed if the list is empty.

 <?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="@android:id/list"
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         />
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
 </LinearLayout>

Here we just contain the list in a simple LinearLayout.

Java

 package com.higherpass.android.widgets.test;
 
 import android.app.ListActivity;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 
 public class TestListActivities extends ListActivity {
         String[] listItems = {"exploring", "android", 
                               "list", "activities"};
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         setListAdapter(new ArrayAdapter(this, 
              android.R.layout.simple_list_item_1, listItems));
     }
 }

Instead of using an Activity we use a ListActivity to extend our main class from. The string array listItems contain the text that will populate the list. As with other activities the onCreate() method is used and the first thing that should be done is call super.onCreate. Next set the main.xml file as the layout to display with setContentView(R.layout.main).

The last thing to do is to populate the list. To populate the list you need to create an ArrayAdapter and pass it to setListAdapter(). ArrayAdapters manage the ListView objects managed by an internal array. The ArrayAdapter constructor expects 3 parameters, the current class instance (this), The display layout to use for each list item, and the array of items respectively. Android comes with a few built-in layouts available under android.R.layout. We used android.R.layout.simple_list_item_1 which displays a single string.

Attaching a Cursor

The CursorAdapter class allows standard Android cursors to be used to populate a ListView. This provides a simple interface to present database data to the user. See the Android cursor tutorial for more information on cursors. Android comes with a bundled ListAdapter for cursors called BasicCursorAdapter To demonstrate using cursors with lists we're going to query the browser bookmarks. For this example we need to grant access to the browser bookmarks in the AndroidManifest.xml file by adding the following line after the uses-sdk statement.

    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
    <uses-permission android:name="com.android.broswer.permission.WRITE_HISTORY_BOOKMARKS" />

This simply grants read and write access to the browser bookmarks and history.

package com.higherpass.android.widgets.test;

import android.app.ListActivity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.Settings;

import android.provider.MediaStore.Audio.Media;
import android.widget.SimpleCursorAdapter;

public class TestListActivities extends ListActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String[] projection = new String[] {Browser.BookmarkColumns._ID, 
                                     Browser.BookmarkColumns.TITLE, 
                                     Browser.BookmarkColumns.URL};
        String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE, 
                                     Browser.BookmarkColumns.URL};
        int[] displayViews = new int[] { android.R.id.text1, 
                                     android.R.id.text2 };

        Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI, 
                       projection, null, null, null);
        setListAdapter(new SimpleCursorAdapter(this, 
                       android.R.layout.simple_list_item_2, cur, 
                       displayFields, displayViews));
   }
}

Basing a List off a cursor is very similar to using an array as before. A few things do need to be setup though. First create a string array called projection populated with Android SDK constants for the database columns that we want to retrieve. Next create a string array called displayFields which holds the fields that will be displayed in the view. The displayFields works with the displayColumns integer array that will tell the view where to place the data from the cursor. The order of the displayFields and displayColumns arrays are important. The positions of each array relate what data gets placed into each view element. In this case the bookmark title goes into the text1 field and the URL goes into the text2 field.

Next create the cursor. For simplicity we're just going to pass the URI of the database and the fields we want to retrieve to the managedQuery that creates the cursor. See cursor article for more information on cursors. Finally create a SimpleCursorAdapter. Similar to the ArayAdapter the first parameter is the current class instance (this). Now pass the autogenerated R variable that corresponds to the view for the row. In this example we used android.R.layout.simple_list_item_2. This is a small preview to the next thing we're going to cover, Customizing the list item display. The android.R.layout.simple_list_item_2 layout is built in to the Android SDK. It has 2 internal TextViews one with large text, followed by one with smaller text below. The third parameter to the SimpleCursorAdapter constructor is the cursor itself. Fourth pass the displayFields string array and finally pass the displayViews.

 1 2 3 4 5  >> Customizing List Appearance
New Content