【问题标题】:Get getCheckedItemPositions() and getCheckedItemIds() always returns null获取 getCheckedItemPositions() 和 getCheckedItemIds() 总是返回 null
【发布时间】:2017-10-05 20:44:24
【问题描述】:

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

格雷格

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

【问题讨论】:

    标签: android listview


    【解决方案1】:

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

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

    • 这将告诉您是否有一个可供提取的填充列表。如果你不这样做,那就是你的问题。

    2) 我还会检查以确保您的 listView 的名称也是正确的。我会假设编译器会捕捉到这一点,但你永远不知道。

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

    对不起,如果这不是特别有用。如果所有这些都检查出来并且问题没有在这些区域之一中产生,则此链接可能会有所帮助:

    Android: list.getCheckedItemPositions() always returns null

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

    【讨论】:

      【解决方案2】:

      最大,

      嗯,您为我指明了“解决方案”的正确方向。这可能不是正确的方法,但鉴于这是我的第一个 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();
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-04
        相关资源
        最近更新 更多