• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get the indexes of ListView items

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic