为什么ListView.getCheckedItemPositions()没有返回正确的价值观?价值观、正确、ListView、getCheckedItemPositions

2023-09-12 01:16:59 作者:旧城失词‖soul ≈

该应用程序与多选一的ListView启用,它工作正常的用户界面。但是,当我读出的值出使用这种code:

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似乎返回false任何不存在的项目,这样的问题的根源似乎是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()返回false值的所有项目。我看不到的方式来使用的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行。因此,从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
    }
      }
);
 
精彩推荐
图片推荐