gpt4 book ai didi

java - getCheckedItemPositions() 总是返回 null (Android ListView)

转载 作者:行者123 更新时间:2023-12-02 00:37:27 25 4
gpt4 key购买 nike

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

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

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

所以问题是:我想用复选框填充 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 将为 null。因此,根据我的了解,这是一个常见的错误,需要一个处理程序作为解决方法。但我无法让处理程序工作,我在 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");
}
});

最佳答案

试试这个代码

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);

//像这样获取选中的项目;

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

关于java - getCheckedItemPositions() 总是返回 null (Android ListView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7402150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com