Another Android Blog

Insights into those hard to solve Android Development problems

Skip to: Content | Sidebar | Footer

A Simple ListView Example

11 June, 2010 (15:12) | Components | By: Randall Mitchell

back to page 2

Listening for a User’s Selection

Now that we have a scrolling list of items, let’s add some functionality to it. I am going to override a method from the ListActivity class (remember our activity class extends this class already). In order to do that, all we have to do here is add a method into our code with the “@Override” compiler directive and our compiler will replace the ListActivity method with ours. The method we are overriding is the onListItemClicked() method. The important think about overriding methods is that we should always call the superclass’s method inside of our new method. This ensures that the parent class is allowed to do what it needs to do before our code executes. Let’s take a quick look at the code.

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    ...
}

onListItemClick takes four arguments. In this case, Android is internalling calling this function and passing it the necessary arguments. What we are concerned about is how handle them inside of this method. The first argument is simple the list that the user clicked. The second argument is view that was clicked within the list. In our case, this should return a TextView. The third argument is the position of the View within the list. The final argument is the “row id” created for the list item by Android. We pass all of those arguments on to the parent class’s onListItemClick() method and now have programming access to the onListItemClick() method.

Now that our activity has access to the method that handles user list item selection, let’s put a little code into it. A typical list like this would route a user to a new activity that would be related to the list item. For the sake of not creating another activity in this example, I am going to create a pop up dialog that informs the patient what they clicked. This way you can see the use of the “getListView().getItemAtPosition(position)” method of accessing the list item. The first thing I do is create a string from the text within the clicked item.

String itemText = getListView().getItemAtPosition(position).toString();

Next I create an AlertDialog to display the text to the user. I am not going to get into the code behind the AlertDialog builder here. Just know that the entire builder code can be replaced with what ever method call you want to happen based on the user clicking on a specific item.

Conclusion

Well, that’s pretty much all there is to it. I have shown a simple example of a ListView and have tried to explain it as thoroughly as possible. I hope this tutorial finds its way onto the monitor of someone who was looking for it. In the near future I hope to post a tutorial on a more advanced ListView. Time to keep on coding and expanding my knowledge of Android. The best to the rest of you. Navigate to the next page for the complete code for this example.

(next… the complete code)

Pages: 1 2 3 4

Comments

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

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 Amrish
Time April 13, 2012 at 10:52 am

Hi, I refer the above example. It’s easy to understand for beginner like me. I have another query is that I want to passing data through Edittext and I want to display in listview. So, can you helpe me to get solution of it?

Thanks,

Amrish Patel

Write a comment





*

Spam Protection by WP-SpamFree