为什么 ListView.getCheckedItemPositions() 没有返回正确的值? [英] Why is ListView.getCheckedItemPositions() not returning correct values?

查看:30
本文介绍了为什么 ListView.getCheckedItemPositions() 没有返回正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用有一个 ListView 启用了多选,在 UI 中它按预期工作.但是当我使用此代码读出值时:

The app has a ListView with multiple-selection enabled, in the UI it works as expected. But when I read the values out using this code:

Log.i(TAG,"Entered SearchActivity.saveCategoryChoice()");
SparseBooleanArray checkedPositions = categorySelector.getCheckedItemPositions();
Log.i(TAG,"checkedPositions: " + checkedPositions.size());

if (checkedPositions != null) {
  int count = categoriesAdapter.getCount();
  for ( int i=0;i<count;i++) {
    Log.i(TAG,"Selected items: " + checkedPositions.get(i));
  }
}

我得到这个输出,不管每个复选框处于什么状态:

I get this output, no matter what state each checkbox is in:

Entered SearchActivity.saveCategoryChoice()
checkedPositions: 0
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false
Selected items: false

SparseBooleanArray 似乎对任何不存在的返回 falseitem,所以问题的根源似乎是 getCheckedItemPositions() 返回一个空数组.该方法的行为就好像 ListView 中没有项目,但实际上有.

The SparseBooleanArray seems to return false for any non-existent item, so the source of the problems seems to be that getCheckedItemPositions() is returning an empty array. The method is behaving as if there are no items in the ListView, but there are.

我可以从文档中看到,当 ListView 未设置为多选时,不会返回任何值,但使用以下语句:

I can see from the docs that no values are returned when the ListView is not set up as multi-select, but it is, using this statement:

categorySelector.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

在我的场景中,我使用的适配器是 ArrayAdapter 的子类,并且(没有任何确凿的证据)我怀疑这可能是原因,但我不明白为什么它不应该工作.

In my scenario, the adapter I'm using is a subclass of ArrayAdapter, and (without any solid evidence) I suspect this may be the cause, though I can't see why it shouldn't work.

推荐答案

我仍然不知道为什么,但在我的场景中,getCheckedItemPositions() 为所有项目返回假值.我看不到使用 ListView 上的方法来获取布尔值的方法.SparseBooleanArray 对象中似乎没有真实世界的数据.我怀疑这可能是因为我的实现有些怪癖,也许我已经继承了 ArrayAdapter.真令人沮丧,像这样的问题真的很浪费时间.

I still do not know why, but in my scenario, getCheckedItemPositions() returns false values for all items. I cannot see a way to use the methods on the ListView to get the boolean values out. The SparseBooleanArray object seems to have no real-world data in it. I suspect this may be because of some quirk of my implementation, perhaps that I've subclassed ArrayAdapter. It's frustrating, issues like this are a real time-drain.

无论如何,我使用的解决方案是在创建 ListView 行时分别将处理程序附加到每个 Checkbox.所以从 ListView.getView() 我调用这个方法:

Anyway, the solution I have used is to to attach a handler to each Checkbox individually as ListView rows are created. So from ListView.getView() I call this method:

private void addClickHandlerToCheckBox(CheckBox checkbox) {
  checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        CheckBox checkbox = (CheckBox)arg0; 
        boolean isChecked = checkbox.isChecked();
        // Store the boolean value somewhere durable
      }
  });
}

这篇关于为什么 ListView.getCheckedItemPositions() 没有返回正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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