xtensivearts - One Leap Ahead
Episode #9 – Lists: Item Clicks

Episode #9 – Lists: Item Clicks

Posted 09/13/2009 - 6:41 pm by Mathias Reisch

In this tutorial we will take a look at how to use ItemClickListeners within ListViews. For this episode we will extend the application we created in episode #7. When the user short-clicks on a list item, a small Toast mesage displays, indicating the list item that has been clicked on. A long-click on a list item can be used to remove that item from the list.

A Few More Things To Note

When using lists it is important to know what the underlying data source looks like. For example, an array of strings (like the one we used in episode #7) is easy to use but on the other hand it is also an immutable object and thus cannot be used if you want to be able to remove items from the list. Therefore you have to either use a different data source or you have to convert your existing array to a mutable object. In this tutorial I will show you how to convert the array from episode #7 to an ArrayList, that can then be used as a new data source for our list.

The Tutorial

If you have a question, criticism or any kind of suggestion, please feel free to leave a comment below. I alway appreciate feedback.

You can find the project files for this tutorial here: Episode #9 Project Files (.zip)

Related posts:
  1. Episode #6 – Notification & Long Clicks
  2. Episode #10 – Intents: An Introduction
  3. Episode #7 – Using Basic Lists
  4. Quick Tip #2 – Sorting Lists
  5. Episode #8 – The Alert Dialog
  6. Episode #11 – Intents: Multi Activity Applications
  7. Episode #3 – Using Buttons
  8. Episode #5 – Using Radio Buttons
  9. Episode #2 – Create A New Project And Run It
  10. Episode #4 – Checkboxes & Complex Layouts

Why Not Leave a Comment?

Name:*
E-Mail:*
Website:
Comment:*

Comments

joshua

11/01/2009 – 4:48 am

Great Stuff.
do you plan to develop more?

Mathias Reisch

11/01/2009 – 1:51 pm

@joshua: Thank you very much! And yes, there will definitely be more tutorials coming up.

KC

03/05/2010 – 12:09 pm

Excellent job! All very clear except: Why Toast.makeText has to use getApplicationContext() instead of context in AlertDialog.Builder() ?
Thx for the video!

John

03/31/2010 – 8:10 pm

Great Mathias! the people ask for new tutorials!!!!!

Hannes

05/28/2010 – 12:21 am

just for the record, as in addition to the context variable that is stored to access the instance of episode9.class, you can refference the ‘this’ from inside the inner scope by typing episode9.this. method or variable
at least i thought you could do so. but the appointed way of refferencing these variables is the most common and maybe the best practice, i don’t know, what do you think?

pranay

06/06/2010 – 7:51 am

its really great and useful , could you please give a tutorial on using databases in android?

Mathias Reisch

06/06/2010 – 4:02 pm

Yo will be happy to hear that an introduction to using databases with Android is coming up next.

Stefan

07/14/2010 – 2:20 pm

Great tutorials!
I wonder if you will make a tutorial on how to parse XML?!

Mathias Reisch

07/14/2010 – 9:57 pm

Hi Stefan,

parsing XML is not really related to Android development but I might slip in some sort of quick tip on how to parse XML with Java.

vinay

07/28/2010 – 7:47 am

very helpful..
thank you so much..

Joseph

08/04/2010 – 7:45 pm

Hi Math! Another great turial. Everything is clear except Context.
Does the context after initialization
” private final Context context = this; ”
contain the same value as
getApplicationContext() method returns?
Why don’t you just use variable context when creating toast in the 8th minute of this tutorial?

Thanks

Jake

08/20/2010 – 7:03 pm

What would I do to go from one list and be able to click on a subject then for it to forward to another list or actual data? I cannot find anywhere to do this. Thanks

Mathias Reisch

08/20/2010 – 10:38 pm

Hi Jake,

you should take a look at ‘Intents’ (covered in episode #10 and #11).

Greetings,
Mathias

Sanket

09/17/2010 – 5:45 am

Awesome tutorials… I m loving it… Wonderful explanation..

sabine

09/23/2010 – 12:57 am

First of all thanks Mathias for these great tutorials. Those are really a good way to start.

shree

12/21/2010 – 1:41 am

Episode #7 – Using Basic Lists & 8 are only half videos. ls upload full videos na.

Chris

01/18/2011 – 1:49 pm

Hi Mathias,
thank you for these great tutorials!

I have one question about this tutorial.
If I have multiple items of the same name in the list, always the first entry is been removed from the adapter.

The adapter.remove() function seems not to check the equality of the string object but of the string itself.

I tried playing around by removing the item from the ArrayList but the adapter seems to with a clone of this list and not the list itself.

Is there a workaround
Thank you!

PS: If you’re answering via mail, you can answer in german.

Oleksandr

01/18/2011 – 3:02 pm

Thanks, pal. Your tutorials accelerate study. Thanks from Ukraine.

Tu Cao Huu

02/04/2011 – 5:17 pm

Thanks a lot!

Sairam

02/05/2011 – 12:13 am

hi mathias,I would love to see some tutorials on sensors. especially related accelerometer..

Hammad

02/18/2011 – 3:09 pm

Great Lecture ,,put some more in depth lecture like that ,,, regards HK.

Sunny

02/18/2011 – 11:53 pm

Hello Mathias, I became a fan of your videos !!!
I wanted to share an idea with you to make the tutorials more interactive and effective learning.

gil

02/21/2011 – 7:53 pm

thx alot hope to see some more tuts in the future

Nguyen Hoang Nam

02/27/2011 – 5:51 pm

It’s great.
Thank you so much!

me

03/19/2011 – 2:28 am

Hello,
Thank You for tutorials I like them all in this site.
I want to ask simple question..
How to make clickable and linked to another list?

Nishant

05/17/2011 – 9:26 pm

i can not gaet this video.it’s showing me ‘the video is unavailable’