我正在使用此代码块来选择我的自定义列表视图的所有复选框:
listview = (ListView) findViewById(R.id.mylist);
adapter = new ListViewAdapter(getParent(), contactList);
listview.setAdapter(adapter);
check_all = (CheckBox)findViewById(R.id.checkbox_all);
check_all.setOnClickListener(new OnClickListener() {
//listview = getListView();
@Override
public void onClick(View v1) {
int size = listview.getAdapter().getCount();
boolean checked = listview.isItemChecked(0);
for(int i=1; i<size; i++) {
listview.setItemChecked(i, checked);
CheckBox check = (CheckBox) listview.getChildAt(i).findViewById(R.id.checkbox); // line no 417
//
// if(checked)
// {
// check.setChecked(true);
//
// }
// check.setChecked(false);
}
}
});
Mylogcat:
05-27 16:10:46.224: E/AndroidRuntime(5870): java.lang.NullPointerException
05-27 16:10:46.224: E/AndroidRuntime(5870): at com.ht.abc.SettingActivity$5.onClick(SettingActivity.java:417)
05-27 16:10:46.224: E/AndroidRuntime(5870): at android.view.View.performClick(View.java:4162)
05-27 16:10:46.224: E/AndroidRuntime(5870): at android.widget.CompoundButton.performClick(CompoundButton.java:105)
05-27 16:10:46.224: E/AndroidRuntime(5870): at android.view.View$PerformClick.run(View.java:17082)
05-27 16:10:46.224: E/AndroidRuntime(5870): at android.os.Handler.handleCallback(Handler.java:615)
05-27 16:10:46.224: E/AndroidRuntime(5870): at android.os.Handler.dispatchMessage(Handler.java:92)
我也尝试过:
代码:
xml中的:
android:choiceMode="multipleChoice"
在我的活动中:
check_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for(int i=0;i< listview.getAdapter().getCount();i++){
listview.setItemChecked(i,b);
}
adapter.notifyDataSetChanged();
}
});
这对我来说也不起作用。
答案 0 :(得分:1)
您有//listview = getListView();
评论,因此在您尝试使用listview
时很可能是空的。取消注释该行。
check_all = (CheckBox)findViewById(R.id.checkbox_all);
check_all.setOnClickListener(new OnClickListener() {
listview = getListView(); // <---- THIS LINE
@Override
public void onClick(View v1) {
int size = listview.getAdapter().getCount();
boolean checked = listview.isItemChecked(0);
for(int i=1; i<size; i++) {
listview.setItemChecked(i, checked);
CheckBox check = (CheckBox) listview.getChildAt(i).findViewById(R.id.checkbox);
//
// if(checked)
// {
// check.setChecked(true);
//
// }
// check.setChecked(false);
}
}
});
答案 1 :(得分:1)
尝试这样的事情
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mData);
setListAdapter(adapter);
getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
CheckBox chkAll = (CheckBox) findViewById(R.id.chk_all);
chkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for(int i=0;i< getListView().getAdapter().getCount();i++){
getListView().setItemChecked(i,b);
}
adapter.notifyDataSetChanged();
}
});
您无需手动选中或取消选中该复选框。在适配器上调用setItemChecked
后跟notifyDataSetChanged