AlertDialog MultiChoiceItems - select 2 items at a time

I have 4 checkboxes in my Alert dialog box: A, B, C and D. I'm trying to get the behavior to make it so that A can only be selected with B, C or D, so there were only 2 selected time.

Will there be a solution to achieve this behavior? I looked at what I can find through Google, but found nothing. I think I will need to use the share settings, and then, by clicking on each of B, C and D, I can save the String in SP, referring to each of B, C, D, then it can check the string in SP every time, when you click, and if one of the values ​​stored in B, C or D is there, uncheck the box that was checked (hope this makes sense), or maybe the best solution? thank

What I still have:

boolean [] checked = new boolean [] {true, false, false, false}; // This is defined at the top of my class, so that as soon as the dialog is first, 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;
}
+4
source share
2 answers

You must update the counter when each item is checked. When it reaches the maximum limit, you can turn off the other checkboxes.

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

+1
source
if(Count == 2){
            cb.setChecked(false);
        }else if(b){
            Count++;
        }else if(!b){
            Count--;
        }

for this you can use the above logic. let count = 0 intial then count the counter at the otem mark.

0
source

Source: https://habr.com/ru/post/1547243/


All Articles