【问题标题】:getCheckedItemPositions() always returning null (Android ListView)getCheckedItemPositions() 总是返回 null (Android ListView)
【发布时间】:2011-09-14 06:25:12
【问题描述】:

编辑:根据问题的演变,我编辑了这个问题。

首先,我知道有一些类似的问题,但每个问题都有特定的区别,这使得答案对我来说毫无用处......

如果有人可以帮助我,我真的很感激,我对这个问题感到非常绝望......

所以问题是:我想用复选框填充 ListView,并且不能使用 simple_multiple_choice_mode(类似的东西),因为我需要手动构建我的 XML 布局 - 所以我使用的是 list_item.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
        <CheckBox 
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/nomeAPP" style="?listItem">
        </CheckBox>

问题是,如果我使用 simple_multiple (..) 模式选项,getCheckedItemPositions 工作正常。如果不这样做(就像我在下面的代码中那样)getCheckedItemPositions 为空。所以从我读到的,这是一个常见的错误,需要一个处理程序作为解决方法。但我无法让处理程序工作,我在 logcat 中遇到 java.lang.NullPointerException 异常。

谁能帮帮我?

我有这个漂亮的小代码:

this.setListAdapter(new ArrayAdapter<String>(this, 
                    R.layout.list_item, aux));

            list.setItemsCanFocus(false);
            list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            list.setSelected(true);
            list.setClickable(true);
            list.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    //  Auto-generated method stub

                    Log.d("checked",""+arg2);        


                }

                public void onNothingSelected(AdapterView<?> arg0) {
                    // Auto-generated method stub
                }

            });


            CheckBox c = (CheckBox) findViewById(R.id.nomeAPP);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    CheckBox checkbox = (CheckBox)arg0; 
                    boolean isChecked = checkbox.isChecked();

                    if(isChecked == true)
                        Log.v("TAG", "true");
                    else
                        Log.v("TAG", "false");
                }
            });

【问题讨论】:

  • 这个问题被编辑了很多次,标记并创建了一个新问题。

标签: java android listview


【解决方案1】:

试试这个代码

list = (ListView) findViewById(R.id.listagem);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.nomeAPP,aux));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

//像这样获取选中的Items;

int count=list.getChildCount();
for(int i=1;i<=count;i++){
     if(list.isItemChecked(i)){
         //do your task/work
    }
}

【讨论】:

  • 很遗憾,还是同样的问题。
【解决方案2】:

您必须在 ListView 上设置choiceMode 属性才能从此方法中获取对象:

http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPositions%28%29

【讨论】:

  • 我使用了另一个答案的建议,但不起作用。
  • 因为您似乎在使用自己的复选框。一旦您将choiceMode 设置为multi,getCheckedItemPositions 将使用ListView 提供的复选框。如果你想在你的列表项布局中提供你自己的 CheckBox,你需要使用 OnCheckedChangedListener 并使用它提供的信息来跟踪检查的内容。
  • 这就是我正在做的事情。你能检查我更新的答案吗?
【解决方案3】:

您的问题是自定义行应该实现Checkable。 阅读this question

【讨论】:

    【解决方案4】:

    看起来是使用setAdapter的一个bug,使用setListAdapter解决了。

    让我们看看,文档说明 ListActivity 应该使用

    list=getListView();
    <ListView android:id="@android:id/list"
    

    所以忘记 = "+@id(....

    现在我们的 Activity 可以知道 List 的“位置”,现在我们可以使用

    setListAdapter(new ArrayAdapter<String>(this, 
                          R.Layout.List_item, aux));
    

    一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2018-01-16
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多