Android CheckBox - 删除以前 setOnCheckedChangeListener [英] Android CheckBox - Removing a previously setOnCheckedChangeListener

查看:22
本文介绍了Android CheckBox - 删除以前 setOnCheckedChangeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用我自定义的 CursorAdapter 显示 ListView 的应用程序.在我的自定义 CursorAdapter.bindView 中,我有一个 CheckBox 对象,我设置了选中的值(基于光标上的列)并设置了一个 clickListener.这是我的代码:

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();
        }
    });

唯一的问题是当 Android 回收我的视图时,onCheckedChangeListener 仍然处于活动状态,因此对 setChecked() 的调用会导致侦听器中的代码运行.我想知道如何在我包含的代码运行之前使 onCheckedChangedListener 无效.

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.

推荐答案

您可以执行以下操作:

// 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.

这篇关于Android CheckBox - 删除以前 setOnCheckedChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆