How to Use ListActivity in Your Android Application

Introduction

 
When you have many Strings or other types 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
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:orientation="vertical"  
  4.      android:layout_width="fill_parent"  
  5.      android:layout_height="fill_parent"  
  6.      >  
  7.      <ListView   
  8.          android:id="@android:id/list"  
  9.          android:layout_width="fill_parent"  
  10.          android:layout_height="wrap_content"  
  11.          />  
  12. </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
  1. public class ListDemoActivity extends ListActivity  
  2. {  
  3.      String listArray[]={"C-Sharp","Android","Java",".Net"};  
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         ArrayAdapter<String> array=new ArrayAdapter<String>(this,  
  11.               android.R.layout.simple_list_item_1, listArray);  
  12.         setListAdapter(array);  
  13.     }  
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:
  1. @Override   
  2. protected void onListItemClick(ListView l, View v, int position, long id)  
  3. {  
  4.       super.onListItemClick(l, v, position, id);  
  5.       Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();  
  6. }   
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. It 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 a 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.


Similar Articles