1

I have a list view where items have a text view and a button. I have managed to make list view onclick and button on click work together. The problem is that when I click the button I don't really know what list view item index it belongs too. Is there any way to know that? I need this to pass it to a "CRUD" for editing, etc...

Notbad
  • 5,244
  • 9
  • 47
  • 86
  • 1
    add the item index in tag of button in getview function – Vishal Pawar Jun 28 '12 at 09:19
  • 1
    One solution is setTag() and getTag() otherwise You can check this link so you will get idea and solution [List View Button Click][1] [1]: http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons – Andy Jun 28 '12 at 09:23

3 Answers3

5

You can use setTag and getTag here to get the position of the Button Clicked in the ListView,

Something like,

button.setTag(position); // in your getView() method

and then,

int cur_pos = (Integer)v.getTag(); // inside onClick of Button in getView() method

Lalit Poptani
  • 66,608
  • 21
  • 157
  • 241
  • Thank you so much! @Prabuddha please do think you could help me here :http://goo.gl/MAjgTh – eddy Sep 20 '14 at 04:27
2

Some options:

  • You can have separate OnClickListener instances for each button.

  • You can call setTag() on your button to store arbitrary data (e.g. index or identifier) and retrieve it later with getTag()

laalto
  • 144,748
  • 64
  • 275
  • 293
1

An other solution would be to reproduce the button click on its parent, when the user tap on the button: http://developer.android.com/reference/android/view/View.html#performClick()

thomasg
  • 571
  • 4
  • 16