I have a listview containing checkbox and textview and I've added a ViewBinder to my adapter and in this viewbinder I've added an onchecked state listener to my checkboxes and in the onchecked state listener I wrote this code:
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String id=buttonView.getTag()+"";
int buttonid=Integer.valueOf(id);
if(buttonView.isChecked()==true){
Log.d("checked","checked");
myDbHelper.MarkAsFavorite(buttonid);
}
else if(buttonView.isChecked()==false){
Log.d("unchecked","unchecked");
myDbHelper.UnMarkAsFavorite(buttonid);
}
cu=myDbHelper.GetCursor();
adapter.swapCursor(cu);
}
};
but with this code when my check box was getting checked it was saved in the database then when I scroll through the listview it gets unchecked all byitself it accesses the code of the unchecked and unchecks the checkbox and save that in the database.
So I fixed it with adding an onclick listener to the checkbox instead of the onchange state listener:
public void onclick(View view) {
String id=view.getTag()+"";
int buttonid=Integer.valueOf(id);
if(((CompoundButton) view).isChecked()==true){
Log.d("checked","checked");
myDbHelper.MarkAsFavorite(buttonid);
}
else if(((CompoundButton) view).isChecked()==false){
Log.d("unchecked","unchecked");
myDbHelper.UnMarkAsFavorite(buttonid);
}
cu=myDbHelper.GetCursor();
adapter.swapCursor(cu);
}
And I have no more problem. But I wanted to know why this change state listener acted that way?
You have this
checkbox
insideListView
as ListView reuses the views for efficiency same checkbox object may provided to multiple ListView items and when it is checked for same check box is unchecked for another.