Adding onClick handler to View in list item


Last Updated:

  1. andywhoa

    andywhoa Well-Known Member This Topic's Starter

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Hi,

    I have a ListActivity where the list items are defined in another XML layout. The list item layout contains an ImageView, a CheckBox, a TextView and such.

    What I want is to set an onClick listener to the CheckBox in each list item. That is easy enough. The trouble is I need that onClick handler to know which position in the list it is.

    I'm attaching the listener to the CheckBox after that list item's View has been inflated in getView of my Adapter. The getView method has a position parameter, but I cannot reference it in the onClick handler for my CheckBox. I understand why, but I don't know how to get around it.

    How do I accomplish this?
     

    Advertisement
  2. meskiukas

    meskiukas Member

    Joined:
    Aug 20, 2010
    Messages:
    13
    Likes Received:
    1
    How about defining an onListItemClick() method in your ListActivity class, and check the checkbox programmatically from that one? That would give you the position, and also increase the surface area the user can click on.
     

Share This Page

Loading...