Problem with checking all checkboxes
I have 2 problems.
The 1st problem.
I have a CheckedTextView "Select All" item and a list of array items set in a adapter of MutipleChoice. I am current trying to set the state of all the checkboxes of the array items to true/false by selecting the CheckedTextView. It works but there's a bug. Such that开发者_高级运维 when the last item of the array is true, the "SelectAll" to true will work, but if the last item of the array is false, it will uncheck all the items. What I want is even if any of the item's state is false, it will be set to true when the checkedtextview is selected to true.The 2nd problem.
I can't seem to set the checkedtextview to true when all of the array item's state is true.Could it be that I'm doing it the wrong way? Help given would be appreciated..
This is my method for the CheckedTextView "Select All"
private void markAll (Boolean state) {
for (int i = 0; i < lv.getCount(); i++) {
lv.setItemChecked(i, state);
}
}
This is the coding for checking of the states of the array items
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
for(int i = 0; i < lv.getCount(); i++) {
//if even 1 of the item is false, the checkedtextview will be set to false
if(lv.isItemChecked(i) == false) {
ctv.setChecked(false);
}
//if all of the item is true, the checkedtextview will be set to true as well
//the coding should be done in the if-else below, if i'm not wrong
if(...) {
}
}
}
EDIT
This is the new code for calling my method ctv.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
ctv.setClickable(true);
if(ctv.isPressed() && ctv.isChecked() == false) {
ctv.setChecked(true);
markAll(true);
}
else {
ctv.setChecked(false);
markAll(false);
}
}
});
This is my xml code for the checkedtextview
<CheckedTextView
android:id="@+id/checkedtext"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:text="Select all"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:clickable="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
Try this reworked logic:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
ctv.setChecked(true); // assume true at first
// if even 1 of the item is false, the checkedtextview will be set to false
for(int i = 0; i < lv.getCount(); i++) {
if(lv.isItemChecked(i) == false) {
ctv.setChecked(false);
break; // exit loop if you have found one false
}
}
}
This will make your ctv
have the checked state if all of the items in the list are checked, otherwise if any are false then ctv
will have the unchecked state.
To control what happens when you click ctv
itself:
ctv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(ctv.isChecked()) {
markAll(true); // ctv is checked, so make all others checked
}
else {
markAll(false); // ctv is unchecked, so make all others unchecked
}
}
});
You don't need to check state of other items here - if it is just a '(de)select all' button then it should just propagate its own state to the other items.
精彩评论