简体   繁体   中英

How to remove value from ArrayList<String> when another checkbox is checked

I have tried a couple of ways to remove the value from the array list once the checkbox is deselected but seems I still have not succeeded.

The code below contains a switch statement that will hold our cases for each box. Basically when the user selects one of the boxes it stores the value into the ArrayList<String> and then I store access it's value in another method using messageTotalTip = selection.get(0);

Thanks in advance!

    /* Create a View for our Check Boxes */
public void selectTip(View view){

    /*Boolean object will check if our checkbox is checked or not*/
    boolean checked =((CheckBox) view).isChecked();

    switch (view.getId()){

        /*Case if 15% is clicked*/
        case  R.id.cb15Percent:
            if(checked){

                /*store our value into the ArrayList*/
                selection.add("15");

                /*To make sure we cant click any other box*/
                cb18Percent.setChecked(false);
                cb20Percent.setChecked(false);
                cbCustomTip.setChecked(false);

                /*Try and make sure that our value is erased if unchecked */
                if(cb18Percent.isChecked() || cb20Percent.isChecked()||cbCustomTip.isChecked()) {

                    selection.remove("15");
                }

            } else {
               selection.remove("15");
            }
            break;

        /*Case if 18% is clicked*/
        case R.id.cb18Percent:
            if(checked){
                /*store our value into the ArrayList*/
                selection.add("18");

                /*Make sure we cant click any other box*/
                cb15Percent.setChecked(false);
                cb20Percent.setChecked(false);
                cbCustomTip.setChecked(false);


                /*Try and make sure that our value is erased if unchecked */
                if(cb15Percent.isChecked() || cb20Percent.isChecked()||cbCustomTip.isChecked()) {

                    selection.remove("18");
                }


            } else {
                selection.remove("18");
            }
            break;

        /*Case if 20% is clicked*/
        case R.id.cb20Percent:
            if(checked) {
                /*store our value into the ArrayList*/
                selection.add("20");

                /*Make sure we cant click any other box*/
                cb15Percent.setChecked(false);
                cb18Percent.setChecked(false);
                cbCustomTip.setChecked(false);


                /*Try and make sure that our value is erased if unchecked */
                if(cb18Percent.isChecked() || cb15Percent.isChecked()||cbCustomTip.isChecked()) {
                    selection.remove("20");
                }

            } else {
                selection.remove("20");
            }
                break;

        /*Case if the custom box is checked*/  /************ Add dialogue box later once smaller bugs are fixed ************/
        case R.id.cbCustom:
            if(checked){
                selection.add("100");

                /*Make sure we cant click any other box*/
                cb15Percent.setChecked(false);
                cb18Percent.setChecked(false);
                cb20Percent.setChecked(false);

                /*Try and make sure that our value is erased if unchecked */
                if(cb18Percent.isChecked() || cb20Percent.isChecked()||cb15Percent.isChecked()) {

                    selection.remove("100");
                }
            }else {
                selection.remove("100");
            }
    }
}

UPDATE: A big thanks to all you guys. I figured out what I was doing wrong. I was using a different approach before I wrote this post that made me use an ArrayList. After seeing some of your guys comments I realized I didn't need the ArrayList anymore.

FIX: Pretty much just set a global variable called messageTotalTip and set the value of it depending on the case.

Thank you all!

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