【发布时间】:2011-09-14 06:25:12
【问题描述】:
编辑:根据问题的演变,我编辑了这个问题。
首先,我知道有一些类似的问题,但每个问题都有特定的区别,这使得答案对我来说毫无用处......
如果有人可以帮助我,我真的很感激,我对这个问题感到非常绝望......
所以问题是:我想用复选框填充 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 为空。所以从我读到的,这是一个常见的错误,需要一个处理程序作为解决方法。但我无法让处理程序工作,我在 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");
}
});
【问题讨论】:
-
这个问题被编辑了很多次,标记并创建了一个新问题。