CursorAdapter 内的 mcheckBox.setOnCheckedChangeListener

发布于 2024-12-21 15:47:08 字数 786 浏览 4 评论 0原文

我有一个使用自定义 CursorAdapter 填充的 ListView。在 BindView 中,我有以下代码:

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

但是,当设置列表中的一个复选框时,运行的代码似乎适用于列表中最后一项的光标位置。因此,在 4 个项目的列表中,

c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID))

应该分别返回 1、2、3、4,选中 4 个框中的任何一个都会给出值 4。有人知道这是为什么吗?

换句话说,列表中每个 CheckBox 的 onCheckedChangedListener 都是相同的。

I have a ListView that is populated using a custom CursorAdapter. Within BindView, I have the following 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();
        }
    });

However, when one of the checkboxes in the list is set, it seems as though the code that runs applies to the cursor's position as of the last item in the list. Thus, in a list of 4 items where

c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID))

should return 1, 2, 3, 4 respectively, checking either of the 4 boxes gives the value 4. Anyone know why this is?

In other words, the onCheckedChangedListener is the same for every CheckBox in the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文