ListView with CheckBox, Check all checkbox 做相反的事情
ListView with CheckBox, Check all checkbox does the opposite
我正在尝试实现一个带有复选框和全选按钮的列表视图。但是,当我选中全选复选框时,会发生相反的情况,所有复选框都会被取消选中,而当我取消选中所有复选框时,这些框就会被选中。这是我的代码:
chkall = (CheckBox) findViewById(R.id.chkAll);
//chkall.setChecked(false);
/** Defining array adapter to store items for the listview **/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, names);
/** Setting the arrayadapter for this listview **/
getListView().setAdapter(adapter);
/** Defining checkbox click event listener **/
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox chk = (CheckBox) v;
int itemCount = getListView().getCount();
for(int i=0 ; i < itemCount ; i++){
getListView().setItemChecked(i, chk.isChecked());
}
}
};
/** Defining click event listener for the listitem checkbox */
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int checkedItemCount = getCheckedItemCount();
if(getListView().getCount()==checkedItemCount)
chkall.setChecked(false);
else
chkall.setChecked(true);
// String attID =scoresDataBaseAdapter.getAttID(perd,mRowId);
//scoresDataBaseAdapter.insertAttScore(perd,mRowId,attID,Sname,idstud,score);
// Toast.makeText(getApplicationContext(), " "+arg0+" "+arg1+" "+arg2+" "+arg3, Toast.LENGTH_SHORT).show();
}
};
/** Getting reference to checkbox available in the main.xml layout */
CheckBox chkAll = ( CheckBox ) findViewById(R.id.chkAll);
/** Setting a click listener for the checkbox **/
chkAll.setOnClickListener(clickListener);
/** Setting a click listener for the listitem checkbox **/
getListView().setOnItemClickListener(itemClickListener);
}
/**
*
* Returns the number of checked items
*/
private int getCheckedItemCount(){
int cnt = 0;
SparseBooleanArray positions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=0;i<itemCount;i++){
if(positions.get(i))
cnt++;
}
return cnt;
}
您可以通过这样做来反转对 setItemChecked
函数的调用:
getListView().setItemChecked(i, !chk.isChecked());
我正在尝试实现一个带有复选框和全选按钮的列表视图。但是,当我选中全选复选框时,会发生相反的情况,所有复选框都会被取消选中,而当我取消选中所有复选框时,这些框就会被选中。这是我的代码:
chkall = (CheckBox) findViewById(R.id.chkAll);
//chkall.setChecked(false);
/** Defining array adapter to store items for the listview **/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, names);
/** Setting the arrayadapter for this listview **/
getListView().setAdapter(adapter);
/** Defining checkbox click event listener **/
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox chk = (CheckBox) v;
int itemCount = getListView().getCount();
for(int i=0 ; i < itemCount ; i++){
getListView().setItemChecked(i, chk.isChecked());
}
}
};
/** Defining click event listener for the listitem checkbox */
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int checkedItemCount = getCheckedItemCount();
if(getListView().getCount()==checkedItemCount)
chkall.setChecked(false);
else
chkall.setChecked(true);
// String attID =scoresDataBaseAdapter.getAttID(perd,mRowId);
//scoresDataBaseAdapter.insertAttScore(perd,mRowId,attID,Sname,idstud,score);
// Toast.makeText(getApplicationContext(), " "+arg0+" "+arg1+" "+arg2+" "+arg3, Toast.LENGTH_SHORT).show();
}
};
/** Getting reference to checkbox available in the main.xml layout */
CheckBox chkAll = ( CheckBox ) findViewById(R.id.chkAll);
/** Setting a click listener for the checkbox **/
chkAll.setOnClickListener(clickListener);
/** Setting a click listener for the listitem checkbox **/
getListView().setOnItemClickListener(itemClickListener);
}
/**
*
* Returns the number of checked items
*/
private int getCheckedItemCount(){
int cnt = 0;
SparseBooleanArray positions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=0;i<itemCount;i++){
if(positions.get(i))
cnt++;
}
return cnt;
}
您可以通过这样做来反转对 setItemChecked
函数的调用:
getListView().setItemChecked(i, !chk.isChecked());