views:

33

answers:

1

I'd like to make a list adapter that formats views like this:

List Entry

I want to be able to fire a different onClick when the user clicks the image. I have defined the onClick on the image itself in the getView() override, but how do I then get the position of the line that was clicked so I can update the record in the database to record the action?

+2  A: 

First, you need the ListView which represents the adapter. If you store this somewhere already, great; if not, you can take the View which is passed to onClick() and call its getParent() method twice (or more, if the image is nested deeper within the clicked item's view) to get the ListView.

From there, call ListView.getPositionForView() on the View passed into onClick(). This will give you an int representing the position of the clicked item within the list adapter. From there, you can do whatever you want with it.

For example:

public void onClick(View v){
    ListView lv = (ListView)(v.getParent().getParent()); // you may need more getParent()s and/or extra casting
    int position = lv.getPositionForView(v);
    /* Do whatever database stuff
     * You want to do
    */
}
tlayton
Aha! getPositionForView was the key. Thanks much!
Josiah Kiehl