get getCheckedItemPositions()和getCheckedItemIds()始终返回null

时间:2014-12-28 18:57:41

标签: android listview

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

Greg

    //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> 

2 个答案:

答案 0 :(得分:1)

我不确定您是否提供了足够的信息来解决问题。但是,这里有一些故障排除步骤可能会帮助您指明正确的方向:

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

  • 这将告诉您是否有要填充的填充列表。如果你没有,那么你的问题就在那里。

2)我还会检查以确保您的listView的名称也正确无误。我认为编译器会抓住这个,但你永远不会知道。

3)最后,我无法查看列表的填充位置和方式。检查并查看其余代码以查看是否可能在其他地方发生了某些事情,这可能是一个好主意。

很抱歉,如果这不是特别有用。如果所有这些都检出并且没有在其中一个区域中创建问题,则此链接可能有所帮助:

Android: list.getCheckedItemPositions() always returns null

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

答案 1 :(得分:1)

最大,

嗯,你指出了正确的解决方案&#39;。这可能不是正确的方法,但鉴于这是我的第一个Android应用程序,我准备继续前进整个上午试图获取仅使用内置方法检查的项目的列表。我修改了您发布的链接中的代码,并提供了下面的代码。它似乎工作。

我想知道我是否会遇到不能同时显示所有内容的列表的问题。我的测试在列表中只有两个项目。如果检查项目然后滚动到视图外,这是否有效。需要进行更多测试。

谢谢,

格雷格

        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();
            }
        }
相关问题