Using ArrayAdapter and ListView in Android Applications

Using ArrayAdapter and ListView in Android Applications

This week’s homework in the android class was to create a simple ListView using ArrayAdapter instead of generic ListAdapter.

ArrayAdapter

ArrayAdapter is a special kind of ListAdapter which supplies data to ListView. You can refer to my notes for last week to know about ListView and ListAdapter. You can also read about ArrayAdapter in android documentation.

Adding views

First create an empty android project. Then edit the main.xml layout file to add a ListView. Then create another layout xml file which will contain the TextView (or any component) that will be displayed within the ListView.

Editing Activity

The next step is to change the generated activity class to extend from ListActivity. This is very important because only a ListActivity will be able to display the ListView.

Binding the adapter

The next step is to bind the ArrayAdapter to the ListActivity. We can do this by calling the setListAdapter() method.

To this method we have to pass an object of type ArrayAdapter. You can pass an object reference to this method or we can even create a new anonymous method like below.

setListAdapter(new ArrayAdapter<string>(this, R.layout.list_item, strings) {
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row;
 
		if (null == convertView) {
			row = mInflater.inflate(R.layout.list_item, null);
		} else {
			row = convertView;
		}
 
		TextView tv = (TextView) row.findViewById(android.R.id.text1);
		tv.setText(getItem(position));
 
		return row;
	}
});

We have to override the getView() method of the ArrayAdapter class to create the TextView (or any other component) which will be created for each list.

Finishing up

So that’s it, you are done. :) All you have to do now is to save the project and run it in the emulator. If everything is done properly, then you can see the list of items displayed in the ListView like below.

android-listview

Source code

I have uploaded the entire project source code into github and you download it from there and verify it with your code.

Try to complete the homework, before the next session and do come back to view the notes and the homework for the next session too. :)

You can also subscribe to my blog’s RSS feed or follow me in Twitter to receive updates about my notes for the next sessions.

Possibly Related posts

Tags: , ,

Share thy love

4 Comments so far

Trackback URI | Follow up comments through RSS Feed | Post a comment

  • tomig says:

    thank you very much… this is the first example of an listView which is working and easy 2 understand!

  • mobibob says:

    Sudarmuthu,

    As tomig stated, I have to reiterate, “this is the first example of an listView which is working and easy 2 understand!”

    All of the other tutorials were a rehash of Google’s which are valuable, but they are built upon a mountain of “simplified” “helper” classes. I wanted to get back to basics as I will be binding some arbitrary data to some simple lists, therefore, I cannot rely upon the SimpleList2Adapter (or whatever).

    I do want to point out a problem I had when I created my layout with the ListView. I like to provide my own @+id and this caused the error below until I looked at your solution and found:

    android:id=”@android:id/list”

    Without this attribute set, I did not understand the error, nor did I know how to fix it:

    08-13 17:49:58.148 E/AndroidRuntime(19308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobibob.android.myapp/com.mobibob.android.myapp.HomeActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is ‘android.R.id.list’

    -Regards,
    mobibob

    • Sudar says:

      Mobibob,

      Nice to know that my article was helpful to you :)

      android:id=”@android:id/list” is a predefined id which is created by the platform. You can either use it or create your own id.

2 Tweetbacks so far

Leave a Comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">