A Simple ListView Example
The ListView is given very little documentation at Android.com. I thought I could explain a little further how a ListActivity class functions. This is a simple example and I may decide to create a more advanced tutorial on this topic in the future. So let’s get to coding.
The XML file
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1.0" android:text="@string/no_list_data" /> </LinearLayout>
We are going to use a linear layout for this example. Inside the LinearLayout container are two separate widgets, a ListView and a TextView. There are a few things we should know about this setup.
Things to know about this XML file
First, the TextView has been given the id “@android:id/empty”. This result is that Android will hide this TextView when the ListView contains data. When the ListView is empty, the TextView will display it’s text. In this case I have created a string inside the strings.xml resource called “no_list_data” that reads “No List Data Present”. Hopefully that is simple enough.
The next thing we should be aware of is the id for the ListView, “@android:id/list”. This is another predefined id from Android and it is designed to enable android to do all of the heavy lifting for this ListView. That makes this format the simplest format for handling a ListView.
Finally, we should be aware of the layout_height and layout_weight for the ListView. During Android IO 2010, there was an entire presentation devoted to ListView. During this presentation, Romain Guy and Adam Powell addressed ListView‘s layout_height. Essentially, they stated that using wrap_content to define ListView‘s height can create a tremendous amount of overhead for your application and should not be done. Their example of a proper way to layout a ListView set layout_height to “0dip” and layout_weight to “1.0″.
(next up… coding the ListActivity)
Comments
Comment from Randall Mitchell
Time October 16, 2010 at 12:44 pm
Thank Mr. Farhan. Nice job. I was planning an example slightly more complicated like this. No need now. if any body else is intereste, the above link shows how to build a listview into a standard activity.
Comment from win
Time March 9, 2011 at 6:03 pm
hi Randall, thank you so much for this tutorial .just what i’m looking for.now i understand how its work. thank you so much.it really help me a lot =)
can i ask you how if we want to display parsed xml file in listview? i use DOMparse to parse xml file from the URL: http://tanjungrhu.jigsy.com/files/documents/event.xml but i don’t know how to display it in ListView. could you help me with it please.thanks
Comment from Randall Mitchell
Time March 9, 2011 at 7:18 pm
Hi win. thanks for visiting. if all you need is one string per list item, you could just create the array and pass along with the listAdapter:
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArray);
Any more than that and you will probably want a custom View object for each part of the data to be displayed. That’s beyond what I could show in a comment. If memory serves me, farhan’s post actually shows how to do that.
http://mfarhan133.wordpress.com/2010/10/14/list-view-tutorial-for-android/
Sorry I am not much more help. I’m in the process of launching a flash related site and need to put all of my free time there.
Best of luck,
Randall
Comment from win
Time March 10, 2011 at 12:13 pm
oh, its ok, i’m sorry if its bother you, anyway, thank you so much for reply to me.i feel so happy for that. i had asking question to some of forum and blog too but still no one reply me, at least you did.thank you =).i will try again to make its work using ListAdapter and thank you again for the awesome tutorial.its help me a lot.
win
Comment from Android
Time March 21, 2011 at 12:41 pm
Nice example, to get through with the listview, like this if we need to check with custom listview of need to go through with multiple listview in a activity means we can go with this link http://android-codes-examples.blogspot.com/2011/03/multiple-listview-and-custom-listview.html
Comment from Randall Mitchell
Time March 21, 2011 at 2:11 pm
useful. thanks. ps. if that’s your post, you might want to look into blogspot code highlighting. it’s pretty easy to plug in a solution and it makes reading the code easier.
Comment from Mr. Ed
Time November 22, 2011 at 10:58 pm
Thanks for the help. I would like to modify this so that a activity is started and the fields are populated from the selection. Can this be down?
Comment from Randall Mitchell
Time November 23, 2011 at 1:05 am
@ Mr. Ed: It sounds like you want to be able to click on the list item and cause a new activity to launch and populate based on the item that is clicked? If that is the case, you should use onItemClickListener(). There is a (quickly found) example on Android People. Inside of onItemClickListener(), you need to create an activity intent, pass it in a particular value that will let your next activity know which item was clicked, and retrieve that value from the other activity when you get there.
- Randall
Comment from unplugged
Time January 31, 2012 at 7:12 am
was really a simple one i ever read !!
Comment from Victor
Time February 5, 2012 at 8:38 pm
In setupListAdapter() you reference android.R.layout.simple_list_item_1 in this
listAdapter = new ArrayAdapter
( this, android.R.layout.simple_list_item_1, spirits );
but I don’t see the code for R.layout.simple_list_item_1 so I don’t understand how this is going to work. Shouldn’t there be a simple_list_item_1.xml? If so, it is not shown.
Comment from Victor
Time February 5, 2012 at 8:57 pm
Oh found it. R.layout.simple_list_item_1 is an android built in.
Comment from farhan
Time October 16, 2010 at 7:57 am
http://mfarhan133.wordpress.com/2010/10/14/list-view-tutorial-for-android/
This a simple android list view tutorial, you can also apply onClick listener,, complete source code is available