Friday, May 21, 2010

How do I – Android: create a ListView control

Android has a nice control to display data as list called ListView. It’s very easy to use. First of all, put a <ListView /> on form (in your markup xml). Then, attach adapter, that will provide data for your list view, in code, hook up click (and long click) listeners, and that’s it. Unless you need something fancy (which you can definitely do :)). Check out the example below.

1. Declaring list view in xml markup:

File: main.xml

<?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="@+id/demoList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FFFFFFFF">
      </ListView>
</LinearLayout>

2. Declaring a list view item – I’m going to use a ArrayAdapter to populate listbox and it requires a TextView resource to be specified as one of the .ctor parameters:

File: list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
      xmlns:android=http://schemas.android.com/apk/res/android
      android:id="@+id/itemText" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="10px">
</TextView>

Please notice that this TextView element was declared in separate file – list_item.xml.

3. Hook up your list view in code:

package org.example.demo;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.ContextMenu.*;
import android.widget.*;
import android.widget.AdapterView.*;

 

public class Demo extends Activity implements OnItemClickListener {
      private final String[] monthList = new String[]
           
{ "January", "February", "March", "April", "May"
              "June", "July", "August", "September",
      
       "October", "November", "December" };

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // Initialize list.
            ListView list = (ListView) this.findViewById(R.id.demoList);
            list.setOnCreateContextMenuListener(this);
            list.setOnItemClickListener
(this);
            list.setAdapter(new ArrayAdapter<String>(this,
               
R.layout.list_item, monthList));
      }

 

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // Handle list clicks.
      }

      @Override
      public void onCreateContextMenu(ContextMenu menu, View v, 
         
ContextMenuInfo menuInfo) {
            // Create context menu.
      }
}

One comment about code: please notice that how we referenced item template – R.layout.list_item!

You can find more information about ListView and Adapter in Adroid Documentation.

Cheers!

No comments :

Post a Comment