views:

434

answers:

1

OLD QUESTION (Already answered by Mark - The answer is to use getTag()):

Let's say I have a ListView with a custom layout for each row having an ImageView and a TextView. Now, when a click is made, I am able to determine which image and which textview were clicked based on the view but if I have to pass this information to say another activity, I need the position. But, how would I get the position of the item clicked in the listview?

REVISED QUESTION:

I am doing the following: Custom ListView with an ImageView and a TextView. I would like to perform different actions depending on whether an image or a text element was clicked. Now, there are two approaches I can take to perform event handling:

Approach #1: Attach an OnItemClick Listener to an item in the ListView and perform a suitable action. Problem: It remains in question as to which element (image or text) was clicked. How would I determine this piece of information and then take the necessary action?

Approach #2: Attach an OnClick Listener to the image and text separately inside each listview item and then perform a suitable action. Problem: I am highly skeptical about the performance of this approach. Added to that, when a click is performed, the row of the item is not being highlighted which makes it highly non-intuitive. How would I make the row highlight in this case?

Any suggestions?

+2  A: 

Attach the position to the row (or one of its widgets) via setTag(), and retrieve it when needed via getTag().

CommonsWare
Thank You so much... Does that mean I can actually set the tag to the item (that holds my entire piece of information) itself? In that case, I don't really have to worry about getting the item's attributes using a position variable... Any suggestions?
Legend
Also, if this whole thing looks complicated, is there a way I can actually get which view was clicked and then take a specific action?
Legend
w/r/t the first comment, I cannot answer that in the abstract. w/r/t the second comment, your OnClickListener tells you what View was clicked in the onClick() method.
CommonsWare
Thanks... So if I add an OnItemClickListener to the listview, it does not tell me which view was clicked (or does it?). I could not find a way to determine which view (that is, imageview or textview) was clicked...
Legend