vendredi 21 août 2015

Android ToggleButton Combination

I'm using 3 ToggleButtons (A,B,C) to filter the content of my array (with an AND condition) and show the result in a listview.

For example if A is true and B,C false... the listview will only show me the items in my array that only contains A. if A,B is true and C is false. The listview will show the items in my array with A AND B.

I'm doing this with onCheckedChangeListener something like:

 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        List<BrandPlanners> filteredList = new ArrayList<>();
        if (isChecked){
            if (buttonView == A) {
                if (B.isChecked()){
                    if (C.isChecked()){
                        //show listview with A,B and C elements
                    } else {
                        // show listview with A AND B elements
                    }
                } else {
                    if (C.isChecked()){
                        // Show listview with A and C elements
                    } else {
                        // Show listview with A elements
                    }
                }
            }
            if (buttonView == B) {
                if (A.isChecked()){
                    if (C.isChecked()){
                        // show listview with 3 elements
                    } else {
                        // show listview whth A and B elements
                    }
                } else {
                    if (C.isChecked()){
                        // show listview with C and B elements                    
                    } else {
                        // show listview with B elements
                    }
                }
            }
            if (buttonView == C) {
                if (A.isChecked()){
                    if (B.isChecked()){
                        // show 3 elements
                    } else {
                        // show C and A elements
                    }
                } else {
                    if (B.isChecked()){
                        // show C and B elements
                    } else {
                        // show C elements
                    }
                }
            }
        } else { //if isChecked() is false
            //create all the if conditions again
        }
    }

I think this is not a good practice, the question is: How can i do this in a better way? this is actually working but i am not happy with the code.

Aucun commentaire:

Enregistrer un commentaire