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.

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.

Related posts

Tags: , ,

26 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.

  • shindow says:

    greate job,dude,thank you so much

  • prashanth says:

    hi,
    can you give an example where i can update the listview dynamically..
    say for example i have a search app, i need to show the results as they are found…

  • C says:

    This tutorial is crap; you speak of using a ListView, but you’re not…you’re using a ListActivity, which means your activity can have only one single list view and zero additional functionality.

  • Vendetta says:

    Hey!

    Could you please explain “row = mInflater.inflate(R.layout.list_item, null);” I am a total noob at android and I don’t know what a inflater is. Thanks!

  • calvin says:

    Thank you, thank you, thank you!!!

    You have no idea how many hours I’ve spent on this. Ironically, this is part of a homework number 2 I am working on as well. The key “thing” you said in your write-up was that you needed a SEPARATE layout .xml for the TextView item that populates the ListView. That was the key… NOWHERE on developer.android does it make that clear.

    again, thanks!

  • Abhijit says:

    “This is very important because only a ListActivity will be able to display the ListView. ” This is incorrect. A ListView can be shown by any activity

  • Heath says:

    Hi,

    Thank you for your post. I found it very helpful. I’ve been trying to find some good examples or something that explains well ListAdapter, however, all the examples on the web are pretty useless. Then I came across your posting and it was well explained. Thank you!!
    Can you help me to clearly understand this… You mentioned that it should be extended from a ListView, and you mentioned the generated Activity. Which one exactly are you talking about ? the mainActivity.Java or the separate class ?

  • Shashank says:

    hi,
    can you give an example where i can update the listview dynamically..
    say for example i have a search app, i need to show the results as they are found…

  • Shashank says:

    I am working on a project that requires me to retrieve info from GAE Datastore and display it on the android app in a list view… can you help me on this pls ?? how do i go about this task.. ?

  • Gayathri says:

    Hey,
    I need to add data to list view from two EditTexts using Add Button.I can add data ,but I can’t increment the row count.So,if I add new one even it is display on previous row without adding new row.Please tell me how can I increase a row…
    Thank you

  • blackbarby says:

    how do i add images to list view??

  • Sapan says:

    Hello,

    I am looking for small story app, I have some images and audio file using that i want create an app for kids, where kids can do horizontal swipe and behind that image the audio file will run describing what image is all about.

    Do you have any sample source code which does the similar stuff on Android, or link to similar open source code will help me to go ahead.

    Thanks,

  • RAGHU says:

    I need explanation & use of ARRAYADAPTER

  • Siddhant says:

    Iam getting this error what to do???
    “The method setListAdapter(new ArrayAdapter(){}) is undefined for the type Main”

  • very nice and informative
    thank you it helped to solve my problem

  • Siddhant says:

    Iam getting this error what to do???
    “The method setListAdapter(new ArrayAdapter(){}) is undefined for the type Main”
    replyy me please…

2 Tweetbacks so far

6 Trackbacks/Pingbacks so far

Leave a Reply