Android Get getCheckedItemPositions()和getCheckedItemIds()始终返回null
我在android应用程序中有一个带有复选框的列表视图,我正在尝试获取一个选中项目的列表。我尝试了使用setChecked(true)在getView()方法中设置checked状态,以及通过点击该项手动检查它。我尝试了两种不同的方法来获取选中的项,并且都返回null。我做错了什么 格雷格Android Get getCheckedItemPositions()和getCheckedItemIds()始终返回null,android,listview,Android,Listview,我在android应用程序中有一个带有复选框的列表视图,我正在尝试获取一个选中项目的列表。我尝试了使用setChecked(true)在getView()方法中设置checked状态,以及通过点击该项手动检查它。我尝试了两种不同的方法来获取选中的项,并且都返回null。我做错了什么 格雷格 //从菜单调用 //首次尝试-已检查始终为空 //在设置适配器之前,我还设置了setChoiceMode=CHOICE\u MODE\u MULTIPLE //并没有设置setChoiceMode 字符串It
//从菜单调用
//首次尝试-已检查始终为空
//在设置适配器之前,我还设置了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个项目数组。然后以字符串形式输出每个项目的索引号,就像第一次尝试时一样。您还可以尝试输出当前项是否已选中
- 这将告诉您是否有一个填充的列表要从中提取。如果你不这么做,那就是你的问题了
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();
}
}