简体   繁体   中英

Trying to make my select all/clear all button check and uncheck checkboxes

I have a button that says Select All . When clicked all the check boxes in my listview should become checked. The same button changes text to Clear All . When clicked all the checkboxes should become unchecked and the text turns back to Select All ....

In my listview however, it's every second button that becomes checked or unchecked.

When Select All is clicked, checkbox 1 , 3 and 5 become checked. Checkbox 2 and 4 become unchecked. text turns to Clear All .

When Clear All is clicked, checkbox 1 , 3 and 5 become unchecked. Checkbox 2 and 4 become checked. text turns to Select All .

I can't see what's wrong with my code. Or maybe it's something in another part of my app but there's no errors or warnings, not sure where to start debugging. Thanks.

   btnCheckAll.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            int count = MatchingContactsAsArrayList.size();
            for (int i = 0; i < count; i++) {
            LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout
            CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);


            if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) {
                checkbox.setChecked(true);
                btnCheckAll.setText("Clear All");
            }


                else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){
                        checkbox.setChecked(false);
                btnCheckAll.setText("Select All");


            }}}});

I think the problem might be here:

if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) {
                checkbox.setChecked(true);
                btnCheckAll.setText("Clear All");
}else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){
                        checkbox.setChecked(false);
                btnCheckAll.setText("Select All");
}

This if is inside a loop, so it is acting as a swich turining on and off on each loop.

On each loop the text "Select All" / "Clear All" will change, so will do the evaluation of the button state in the next loop.

The condition inside the for loop keeps on Changing from true and false because the first loop changes the Text the second loop changes it again and it keeps on being changing in this part ( Read the comments! ):

if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) {//This condition will be true on first round and 3 and 5 and 7...
            checkbox.setChecked(true);
            btnCheckAll.setText("Clear All");
        }


            else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){//And this one will be true in 2 and 4 and 6...
                    checkbox.setChecked(false);
            btnCheckAll.setText("Select All");

SOLUTION:

btnCheckAll.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        boolean toCheck=true;

        //THE CONDITION SHOULD BE OUTSIDE THE LOOP!

 if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) {
            toCheck=true;
            btnCheckAll.setText("Clear All");
        }


            else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){
                    toCheck=false;
            btnCheckAll.setText("Select All");


        }



        int count = MatchingContactsAsArrayList.size();
        for (int i = 0; i < count; i++) {
        LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);
        checkbox.setChecked(toCheck);
    }}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM