Android的复选框 - 删除previously setOnCheckedChangeListener [英] Android CheckBox - Removing a previously setOnCheckedChangeListener

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

问题描述

我有使用我自定义一个CursorAdapter的显示ListView控件的应用程序。在我的自定义CursorAdapter.bindView,我有我设置的检查值(根据光标列)一个CheckBox对象,并设置一个clickListener。这是我的code:

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()使听者内的code运行。我想知道如何前右侧的code我已经包括运行invalidata的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();
    }
});

有很多的优化可以做,以高于code的含量。我刚刚说明的基本逻辑。

There are lots of optimizations you can do to above code. I just illustrated the basic logic.

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

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