How to Use ListActivity in Your Android Application


Introduction

When you have many Strings or other type of values to display in a list then a ListActivity is used. It will simply display all the values in a list and you can also use its various types of listeners.

A ListActivity needs an Adapter to set its List. It may be an ArrayAdapter or a SimpleCursorAdapter. In this tutorial, we will see how to use an ArrayAdapter in a ListActivity.

Step 1:

In this step, first of all, create an Android project as in the following.

1.png


2.png

3.png

Step 2:

Then open the "ListDemo->res->layout->main.xml" file. Replace all the content with the following code.

4.png

================

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="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         />

</LinearLayout>

================

In this file, there is one ListView, which holds a list of values in it. This will help us to set content in it.

ListActivity needs a ListView which can bind various sources; either array or cursor. Android provides some standard layout resources which reside in "android.R.layout". They have names such as "simple_list_item_1", "simple_list_item_2" etc. To bind that standard layout with your view objects, you need to specify the "id" of your view objects like "@android:id/list" for ListView. This ListView will bind with a standard layout list.
 

 Step 3:

  • Open your "ListDemo -> src-> "com.test" -> ListDemoActivity.java" file.
  • Change "Activity" extends to "ListActivity"
  • Then write down the following code.
================

ListDemoActivity.java

================

public class ListDemoActivity extends ListActivity

{

     String listArray[]={"C-Sharp","Android","Java",".Net"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
main);

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

    }
}

Description:

  • In this code, you can see that we have set "setContentView(R.layout.main)" which we have created in above step. (Step - 2 )
  • Then we take an ArrayAdapter object with a cast to a "String" because we want to use an array of String values.
  • ArrayAdapter has 3 arguments.

 public ArrayAdapter (Context context, int textViewResourceId,String[] object)

context

The current context.

textViewResourceId

The resource ID for a layout file containing a TextView to use when instantiating views.

objects

The objects to represent in the ListView.

And in the last statement, we set "setListAdapter" as "ArrayAdapter" object "array".

Testing:

If you run this program now, you can see the following screen in your emulator. You need to create an AVD (Android Virtual Device) of Version 2.2 or higher.

5.png

Now, click on any item. Does anything happen? Oops.

So, the solution for the click event is that, ListActivity already has a method. You just need to implement it.

protected void onListItemClick(ListView listView, View view, int position, long id)

ListView

The ListView where the click happened

view

The view that was clicked within the ListView

position

The position of the view in the list

id

The row id of the item that was clicked

So use the following code after the "onCreate" method:

@Override 

protected void onListItemClick(ListView l, View v, int position, long id)
{
     
super.onListItemClick(l, v, position, id);
      Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.
LENGTH_SHORT).show();

Description:

In this code, you can see that, we took "Toast" to show text on the screen. Toast has a "makeText" method to show text on the screen having the following arguments:

context

The context to use. Usually your Application or Activity object.

text

The text to show. Can be formatted text.

duration

How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Now, its time for testing. Run your program and click on any item. You will see the item's value on the screen as seen below!

6.png

Summary

In this tutorial, we learned what an ListActivity is and how to set an Adapter in a List. What type of Adapter can we use in it and finally how to get items on a Click event.

Up Next
    Ebook Download
    View all
    Learn
    View all