3

Hello I have a listview in which I am using custom adapter & cutom layout of rows. I have like imageview at the top then bottom of it one checkbox at left end and button on right end.

Now I want to apply checked listener on checkboxand click listener on button. Here is this I am doing

mybutton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
       // TODO Auto-generated method stub

   }
});

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
       if (isChecked){
           // perform logic
       }
   }
});

Question

So in a listview there would be checkbox and buttton in each row. How would I know user click on second or third etc row checkbox or button.

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • Check this answer by blackbelt: http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android good luck! – Thiago Souto Nov 09 '14 at 12:09
  • @Souto No don't want to apply itemclicklistener on listview. Instead of it, in a rows there are views like a button and checkbox in each row and i am trying to apply click listener on them & detect on which row button user clicked – N Sharma Nov 09 '14 at 12:42

1 Answers1

2

There is some ways to do this. My prefferred method is by adding setOnCheckedChangeListener to your checkbox's holder in your custom view class, in getView method.

You can use position parameter in the getView to get the current row position.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • If I scroll the list at the end & click on top row then I don't think I will get the position of clicked checkbox or button. – N Sharma Nov 09 '14 at 09:51
  • @Williams You will get it :) Because getView called multiple times – Blaze Tama Nov 09 '14 at 09:53
  • Suppose on a screen there is 9 rows and `getview` call 9 times here. when I click on 7th checkbox listener then `getview` won't call on click it then what position value I will get – N Sharma Nov 09 '14 at 09:55
  • the position in your onClick will be 7, it will be fired. I used this method for my projects, with very long listview and its working fine. – Blaze Tama Nov 09 '14 at 10:01
  • @Williams Its not that hard to move your code to the custom adapter, why dont you try it? Post your custom adapter code if its still not working – Blaze Tama Nov 09 '14 at 10:01
  • @Williams look at the 1st answer here : http://stackoverflow.com/questions/22946110/set-onclicklistener-in-getview-with-viewholder-class Thats how i did it – Blaze Tama Nov 09 '14 at 10:02
  • @Williams Great :) I dont recommend other ways that involved doing "findviewbyid" to your views in each list item (bad performance). I think this solution is the best for performance – Blaze Tama Nov 09 '14 at 10:06