Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android CheckBox - Removing a previously setOnCheckedChangeListener

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.

like image 361
finiteloop Avatar asked Dec 16 '22 06:12

finiteloop


1 Answers

You can call mcheckBox.setOnCheckedChangeListener(null); if it is done inside the onCheckedChangeListener, you need to declare mCheckBox final.

like image 99
MByD Avatar answered Apr 06 '23 00:04

MByD