Android Get getCheckedItemPositions()和getCheckedItemIds()始终返回null

Android Get getCheckedItemPositions()和getCheckedItemIds()始终返回null,android,listview,Android,Listview,我在android应用程序中有一个带有复选框的列表视图,我正在尝试获取一个选中项目的列表。我尝试了使用setChecked(true)在getView()方法中设置checked状态,以及通过点击该项手动检查它。我尝试了两种不同的方法来获取选中的项,并且都返回null。我做错了什么 格雷格 //从菜单调用 //首次尝试-已检查始终为空 //在设置适配器之前,我还设置了setChoiceMode=CHOICE\u MODE\u MULTIPLE //并没有设置setChoiceMode 字符串It

我在android应用程序中有一个带有复选框的列表视图,我正在尝试获取一个选中项目的列表。我尝试了使用setChecked(true)在getView()方法中设置checked状态,以及通过点击该项手动检查它。我尝试了两种不同的方法来获取选中的项,并且都返回null。我做错了什么

格雷格

//从菜单调用
//首次尝试-已检查始终为空
//在设置适配器之前,我还设置了setChoiceMode=CHOICE\u MODE\u MULTIPLE
//并没有设置setChoiceMode
字符串ItemsChecked=null;
ListView ListView=(ListView)findViewById(R.id.ListList);
SparseBooleanArray checked=listview.GetCheckEditePositions();
对于(int i=0;i
我不确定您是否提供了足够的信息来解决此问题。但以下是一些故障排除步骤,可以帮助您找到正确的方向:

1) 不要根据listView中选中项目的数量获取数组,而是查看是否可以在整个列表中提取项目数组,或者如果列表非常大,至少可以提取前100个项目数组。然后以字符串形式输出每个项目的索引号,就像第一次尝试时一样。您还可以尝试输出当前项是否已选中

  • 这将告诉您是否有一个填充的列表要从中提取。如果你不这么做,那就是你的问题了
2) 我还要检查以确保listView的名称也正确。我想编译器会捕捉到这个,但你永远不知道

3) 最后,我看不出列表在哪里或如何填充。最好检查并查看代码的其余部分,看看是否在其他地方发生了什么

抱歉,如果这不是特别有用的话。如果所有这些问题都得到了解决,并且问题没有在其中一个区域产生,则此链接可能会有所帮助:

祝你好运,我希望这有点帮助。

Max

好吧,你为我指出了一个“解决方案”的正确方向。这可能不是正确的方法,但考虑到这是我的第一个android应用程序,我已经准备好在花了整个上午的时间试图获取一个列表,其中只包含那些通过内置方法检查的项目。我修改了你发布的链接中的代码,并得出了下面的代码。它似乎起作用了

我想知道如果列表不能同时显示所有内容,我是否会遇到问题。我的测试列表中只有两项。如果项目被选中,然后滚动出视图,这将起作用。需要做更多的测试

谢谢

格雷格

ListView ListView=(ListView)findViewById(R.id.ListList);
观点五;
复选框ck;
文本视图电视;
对于(int i=0;i
    //Called from a menu

    //First attempt - checked is always null
    //I also set setChoiceMode = CHOICE_MODE_MULTIPLE before setting adapter
    //and set no setChoiceMode
    String ItemsChecked = null;
    ListView listview = (ListView) findViewById(R.id.ListLists);

    SparseBooleanArray checked = listview.getCheckedItemPositions();

    for (int i = 0; i < checked.size(); i++) {
        if (checked.get(i)) {
            ItemsChecked += Integer.toString(i) + ",";
        }
    }

   //I then tried this and checked[] is empty
   //I also set setChoiceMode = CHOICE_MODE_NONE before setting adapter
   long checked[] = listview.getCheckedItemIds();

    for (int i = 0; i < checked.length; i++) {
        ItemsChecked += "" + checked[i] + ",";
    }


//Layout for the ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_listit" >
    </ImageView>
        <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="22sp" >
    </TextView>

</LinearLayout> 
        ListView listview = (ListView) findViewById(R.id.ListLists);
        View v;
        CheckBox ck;
        TextView tv;
        for (int i = 0; i < listview.getCount(); i++) {
            v = listview.getChildAt(i);
            ck = (CheckBox) v.findViewById(R.id.checkBox1);
            tv = (TextView) v.findViewById(R.id.label);
            if (ck.isChecked()) {
                Toast.makeText(getApplicationContext(), tv.getText() + "Checked" , Toast.LENGTH_LONG)
                .show();
            }
            else {
                Toast.makeText(getApplicationContext(), tv.getText() + "Not checked" , Toast.LENGTH_LONG)
                        .show();
            }
        }