4

I have 4 checkboxes in my Alert Dialog: A, B, C and D. I am trying to get the behavior of making it so that A can only be selected with B, C or D, so there's only 2 selected at one time.

Would there be a solution to be able to achieve this behavior? I have looked at what I could find through Google, but I couldn't find anything. I think I am going to need to use share preferences, then once each of B, C and D are clicked, I could store a String in SP referencing to each of B, C , D, then it could check for the string in SP everytime one is clicked, and if one of the values stored from B,C or D are in there, uncheck that tick box from the one that has been ticked(hope this makes sense), or could there be a better solution? thanks

What I have so far:

boolean[] checked = new boolean[] { true, false, false, false }; //This is defined at the top of my class, so that once the dialog is first opnened, A is selected by default

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.filteroptions, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.filter:

        AlertDialog dialog;

        final CharSequence[] items = { "A", "B", "C", "D" };
        // arraylist to keep the selected items
        final ArrayList<Integer> seletedItems = new ArrayList<Integer>();

        shfObject = getActivity().getSharedPreferences("NAME",
                Context.MODE_PRIVATE);
        final SharedPreferences.Editor shfEditorObject = shfObject.edit();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Filter");



        builder.setMultiChoiceItems(items, checked,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            int indexSelected, boolean isChecked) {

                        if (isChecked) {
                            seletedItems.add(indexSelected);

                            switch (indexSelected) {

                            case 0:
                                shfEditorObject.putString(
                                        "checkbox1Ticked", "Ticked1");

                                shfEditorObject.commit();
                                break;

                            case 1:

                                shfEditorObject.putString(
                                        "checkbox2Ticked", "Ticked2");

                                shfEditorObject.commit();

                                break;

                            case 2:

                                shfEditorObject.putString(
                                        "checkbox3Ticked", "Ticked3");

                                shfEditorObject.commit();

                                break;

                            case 3:

                                shfEditorObject.putString(
                                        "checkbox4Ticked", "Ticked4");

                                shfEditorObject.commit();

                                break;

                            }

                        }

                        else if (seletedItems.contains(indexSelected)) {
                            // Else, if the item is already in the
                            // array, remove it
                            // write your code when user Uchecked the
                            // checkbox

                        }

                    }
                })
                // Set the action buttons
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on OK
                                // You can write the code to save the
                                // selected item here

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on Cancel

                            }
                        });

        dialog = builder.create();// AlertDialog dialog; create like this
                                    // outside onClick
        dialog.show();

        return true;

    default:
        break;
    }

    return true;
}
Jack
  • 2,043
  • 7
  • 40
  • 69
  • You may try onCheckedChanged() Event to check number of Checkbox selected. – Jeeten Parmar Jul 08 '14 at 11:42
  • 1
    Why do you have 5 booleans when you have only 4 Checkboxes? – Simas Jul 08 '14 at 11:42
  • Please update your question as it doesn't make sense. You're saying that A can only be selected with B/C/D but later in the comment you say that A is selected by default. How is that? – Simas Jul 08 '14 at 11:47
  • What happens if A is deselected? B,C,D can't be selected anymore? To make it more clear you could write out the possible combinations of the checkboxes. – Simas Jul 12 '14 at 09:41

2 Answers2

1

You should update a counter when each item is checked. When it reaches the maximum limit, you can disable the other checkboxes.

This question answers it nicely- How to disable checkbox when a limit is reached in android?

Community
  • 1
  • 1
EatHeat
  • 80
  • 8
1
if(Count == 2){
            cb.setChecked(false);
        }else if(b){
            Count++;
        }else if(!b){
            Count--;
        }

you can use the above logic for it. let count = 0 intial then make count increment on otem checked.

Jabbir Basha
  • 455
  • 6
  • 7