This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Get ListView checkbox ID and item ID

 
Ranch Hand
Posts: 202
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I have a custom list and i want all checked items' ID, don't know why but all i have ordered number when onclicked item and non of checkbox id.

Thanks for any help.

This is my adapter :


And this is my activity :
device-2014-02-19-004601.png
[Thumbnail for device-2014-02-19-004601.png]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ibrahim yener wrote:Hello

I have a custom list and i want all checked items' ID, don't know why but all i have ordered number when onclicked item and non of checkbox id.



I am having a hard time interpreting what this means. But here is what I think it means, based on the code:

"I have a custom view that has a checkbox in it and that I add to a ListView. I have an onItemListClick() listener attached to the ListView. When I get the event for onItemClicked, I get the position of the custom view the user clicked on, not the ID of the checkbox. How do I get the ID of the checkbox?"

Getting the position of the clicked-on row is what you are supposed to get, see the API: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html. And if you think about it, it should make sense:
1- Your clicked on item in the ListView is the custom view - your custom view can have many ids. How is the framework supposed to know which view's id you want?
2- The item you are clicking on, relative to this event, is the custom view, not the checkbox
3- A ListView uses an adapter and the adapter often builds the view that gets displayed rather than getting from an XML file, so any given view in the ListView may not have an ID.
4- A ListView recycles its views, you may have multiple views on the screen with the same ID because they came from the same layout, and one instance of the view may be used to display multiple different rows as you scroll through the list (views that go invisible will be reused to view those rows that come into view). So providing just the View doesn't completely tell the story, you need to know the position in the greater context of the underlying data.

You can get the checkbox view for a given row by doing the findViewById() method on the view argument passed to the onItemClickListener(). But that is not (only) how you should track which items are checked and which ones are not since those checkboxes may be reused (they are not reliable datastorage, they are view). Rather, you should store the state inside the adapter and create a method which you can use to toggle the internal state of that row in the adapter (then update the view to reflect the state).

For example, in your adapter create a boolean array that defines the selected/not selected state for each data element. Create a method that takes the position of the element to toggle its state and the view for the row. In the method toggle the value inside the boolean array, then get the tag for the row's view, get the checkbox from the tag, and set its state to the same as in the boolean array. In your onItemClick() method, call this new method on your adapter.

ibrahim yener wrote:Thanks for any help.

This is my adapter :


And this is my activity :

 
ibrahim yener
Ranch Hand
Posts: 202
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Steve
Sorry for giving you hard time, also thanks for reply.

I have followed your suggestion and solved problem.
I am in the office now, i will post solution when i back to home.

Thank you for your help.
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic