我標記了要刪除的另一個線程,因爲主要問題被編輯了很多次,以致它變得混亂。如何獲取setOnCheckedChangeListener(ListView複選框工作)
所以問題是:我想用複選框填充一個ListView,因爲我想自定義我的ListView我沒有使用simple_multiple_choice_mode,我把我的佈局放在list_item.xml中:(對於每一行)
<CheckBox>
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
android:layout_height="wrap_removed" android:id="@+id/nomeAPP" style="?listItem">
</<CheckBox>
我的ListView是lista.xml
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_height="wrap_content"
style="?whiteBackground">
我的適配器:
list=getListView();
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, aux))
所以我想爲複選框做一個setOnCheckedChangeListener,所以我可以將選擇的項存儲在一個數組中。
所以我做了這個監聽器:
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "UnChecked",
Toast.LENGTH_SHORT).show();
}
}
});
問題是:我得到的NULL例外。該好奇的事情是:如果我一個簡單的
<ScrollView android:id="@+id/widget54" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<CheckBox android:text="Checkbox" android:id="@+id/CheckBox01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
</ScrollView>
「開關」我的佈局,而忘記我的ListView(和我的轉接器),它的工作原理!那麼,我的佈局有什麼問題?
我創建了一個新的小項目,只是爲了讓大家看清楚情況。它可用HERE現在不起作用,但如果您取消註釋適配器,將ListActivity更改爲活動並更改setContentView,它將起作用(選中/取消選中Toast文本)。
我發現另一個奇怪的是,與ListActivity如果它的相關不從未工作....不知道...
你能發佈null異常日誌嗎? –