Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get the indexes of ListView items

 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have ListView activity and it has 3 items on it, I want that if the user click on each one of them the related activity to the list’s item be shown to the user. How can I do this? Could anyone help me to fix this?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would set an AdapterView.OnItemClickListener for the Listview - its onItemClick callback has an "int position" parameter.
 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code below, but as you said I don't know how to add the code you mentioned into my code?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't have the activity implement that interface, but instead use an inner class. Something like this:

 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply, but I want a sample code for, if the user for example clicks on the first item, activity1 starts and if the user clicks on the second item, activity 2 starts and ...! I mention that the items' names are in persian mode not English! How can I do this? I would appreciate if you tell me the answer!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you know the position of the item being clicked on, what specific difficulty are you facing in starting activities based on that position? The mapping should be between numbers and activities, not between strings and activities.
 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've wrote the code below

As I wrote in the code, if the user clicks on each item, he could see the items name like Toast. but I want that if the user for example clicks on the first item, it shows an activity1 and if the user clicks on the second item, it shows activity2. but in the code above I don't know how to add this?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work through https://developer.android.com/guide/components/activities.html. It explains how to start activities programmatically.
 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want some code like the below:

if (option 1 clicked by the user)
activity1 starts;
if (option 2 clicked by the user)
activity2 starts;
...
I don't know how to make diffrence between option #1 and option #2 and option #3 in the listView.
How can I do this for a ListView activity?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to make diffrence between option #1 and option #2 and option #3 in the listView.

You don't? You have the number 1 or 2 or 3 (the "position" parameter in the onItemClick callback); what else do you need?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic