如何取消setItemChecked()选中的项目?

发布于 2024-12-18 22:27:19 字数 265 浏览 2 评论 0原文

我使用带有 MULTIPLE_CHOICE 的 ListView 并使用 setItemChecked() 方法取回所选项目。

它工作正常,因为我可以看到以前检查的项目。 问题是,如果我取消选中其中一个之前选中的项目,然后通过custList.getCheckItemIds()获取选中项目的列表 该数组仍然有我未选中的项目。

谁能告诉我这是否应该发生或者我错过了什么?

I am using a ListView with MULTIPLE_CHOICE and to get the selected items back i am using setItemChecked() method.

It works fine as i am able to see the previously checked items.
The issue is that if i uncheck one of the previously checked items, and then get the list of checked items by custList.getCheckItemIds()
the array still has the Item that i unchecked.

Can anyone please tell me if that is supposed to happen or am i missing something?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2024-12-25 22:27:19

在这里,您必须调用 setOnCheckedChangeListener 并且必须管理此侦听器块内的代码。

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              // Write and manage your code here.
        }
});

Here you have to call setOnCheckedChangeListener and you have to manage the code inside this listener block.

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              // Write and manage your code here.
        }
});
烟若柳尘 2024-12-25 22:27:19

如果您只是想找出在任何给定时间检查了哪些项目,您可以从 ListView 获取 SparseBooleanArray,并使用 for 循环对其进行迭代。例如:

SparseBooleanArray checked = list.getCheckedItemPositions();

for (int i = 0; i < checked.size(); i++){
    if (checked.get(i))
        //the item at index i is checked, do something
    else
        //the item is not checked, do something else
}

If you are simply trying to find out what items are checked at any given time, you can get a SparseBooleanArray from the ListView, and iterate over it with a for loop. For example:

SparseBooleanArray checked = list.getCheckedItemPositions();

for (int i = 0; i < checked.size(); i++){
    if (checked.get(i))
        //the item at index i is checked, do something
    else
        //the item is not checked, do something else
}
め七分饶幸 2024-12-25 22:27:19

这:

SparseBooleanArray checked = list.getCheckedItemPositions();

for (int i = 0; i < checked.size(); i++){
    if (checked.get(i))
        //the item at index i is checked, do something
    else
        //the item is not checked, do something else
}

不起作用。

按照 多个联系人选择器列表 [getCheckedItemPositions()]

应该没问题。

SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();

for (int i=0; i<selectedPositions.size(); i++) {
    if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
        //do stuff
    }
}

this:

SparseBooleanArray checked = list.getCheckedItemPositions();

for (int i = 0; i < checked.size(); i++){
    if (checked.get(i))
        //the item at index i is checked, do something
    else
        //the item is not checked, do something else
}

doens't work.

follow Multiple Contact Picker List [getCheckedItemPositions()]

should be OK.

SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();

for (int i=0; i<selectedPositions.size(); i++) {
    if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
        //do stuff
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文