I flagged the other thread to be deleted, because the main question was edited so many times, that it become confusing.
So the problem is: I want to populate a ListView with checkboxes, and as I want to customize my ListView I'm not using simple_multiple_choice_mode, I'm putting my layout on list_item.xml: (for each row)
<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>
My ListView is on lista.xml
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_height="wrap_content"
style="?whiteBackground">
my adapter:
list=getListView();
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, aux))
So I'm trying to make a setOnCheckedChangeListener for the checkboxes, so I can store the chosed items in an array.
So I did this listener :
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();
}
}
});
Problem is: I get the NULL exception. The CURIOUS thing is: if I "switch" my layout for a simple
<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:l开发者_运维问答ayout_height="fill_parent">
<CheckBox android:text="Checkbox" android:id="@+id/CheckBox01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
</ScrollView>
And forget my ListView (and my adapter), it works! So, what's wrong with my layout?
I created a new little project just to let you guys see exactly the situation. It's available HERE Right now does not work, but if you uncomment the adapter, change ListActivity to Activity and change setContentView, it will work (checked/unchecked toast text).
Another curious thing I found is that with ListActivity does not work never.... wondering if it's related...
ok, i think i got your problem,
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
This works in the second layout, because you might have already set the view for the activity using setcontentview().
So , the problem in your list activity is that , the above mentioned line doesn't know , which layout it has to look for to find id,
R.id.CheckBox01
you have to get access to the layout view which you are inflating the listview , lets call it a inflatedView
, and then you access the check box like this.
CheckBox cb = (CheckBox) inflatedView.findViewById(R.id.CheckBox01);
if you don't set the content view, you have to tell the function findViewById(), where it has to look for the view. I would confirm this, if you post the NPE log.
HTH.
精彩评论