I have an application that displays a ListView using a CursorAdapter that I have customized. Within my custom CursorAdapter.bindView, I have a CheckBox object that I set the checked value (based on a column on the cursor) and set a clickListener. Here is my code:
CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.list_done);
mCheckBox.setChecked(isDone);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
AW.getDB().updateTask(c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID)), isChecked);
TD.displayTasks();
}
});
The only problem is that when Android recycles my views, the onCheckedChangeListener is still active, and thus the call to setChecked() causes that code within the listener to run. I would like to know how to invalidata the onCheckedChangedListener right before the code I have included runs.
You can do something like:
// c is the Cursor you are getting
CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.list_done);
mCheckBox.setTag(new Integer(c.getPosition());
mCheckBox.setChecked(isDone);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Integer posInt = (Integer)buttonView.getTag();
int pos = posInt.intValue();
c.moveToPosition(pos);
AW.getDB().updateTask(c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID)), isChecked);
TD.displayTasks();
}
});
There are lots of optimizations you can do to above code. I just illustrated the basic logic.