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.
In this step, first of all, create an Android project as in the following.
Then open the "ListDemo->res->layout->main.xml" file. Replace all the content with the following code.
<?xml version="1.0" encoding="utf-8"?>
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.
- Open your "ListDemo -> src-> "com.test" -> ListDemoActivity.java" file.
- Change "Activity" extends to "ListActivity"
- Then write down the following code.
public class ListDemoActivity extends ListActivity
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
ArrayAdapter<String> array=new ArrayAdapter<String>(this,
- 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)
The current context.
The resource ID for a layout file containing a TextView to use when instantiating views.
The objects to represent in the ListView.
And in the last statement, we set "setListAdapter" as "ArrayAdapter" object "array".
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.
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)
The ListView where the click happened
The view that was clicked within the ListView
The position of the view in the list
The row id of the item that was clicked
So use the following code after the "onCreate" method:
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();
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:
The context to use. Usually your Application or Activity object.
The text to show. Can be formatted text.
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!
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.