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.

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.

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:

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.

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 ?

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

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.