Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the checked items from a multi choice item in a Alert.Builder?

Is there a way to get some infomation about the checked items in the next Alert.Builder? I need to save some booleans in SharedPreferences when somebody clicked the positive button. Those booleans are from the choices the user made in the alert. How can I get them?

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final SharedPreferences preferences = getSharedPreferences("type_settings", MODE_PRIVATE);

        boolean[] selectedTypes = getSelectedTypes(preferences);

        builder.setIcon(R.drawable.menu_type)
                .setTitle(R.string.list_dialog_title)
                .setMultiChoiceItems(R.array.select_type_items, selectedTypes,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                            }
                        })
                .setPositiveButton(R.string.types_save, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        SharedPreferences.Editor prefEditor = preferences.edit();
                    }
                })
                .setNegativeButton(R.string.types_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                })
                .create();
        builder.show();
like image 213
DaJackal Avatar asked Aug 28 '11 12:08

DaJackal


2 Answers

You could use getCheckedItemIds() or getCheckedItemPositions () to get a list of checked items from the listview. You should use AlertDialog.getListView() to get the dialog's listview first.

mAlert.getListView().getCheckedItemPositions ();
like image 106
Ronnie Avatar answered Nov 03 '22 18:11

Ronnie


I managed to resolve the problem in some way, but i don't know if it's a good practice. Here is the code:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final SharedPreferences preferences = getSharedPreferences("type_settings", MODE_PRIVATE);

        final String[] availableTypes = getResources().getStringArray(R.array.select_type_items);
        final boolean[] selectedTypes = getSelectedTypes(preferences, availableTypes);

        builder.setIcon(R.drawable.menu_type)
                .setTitle(R.string.list_dialog_title)
                .setMultiChoiceItems(R.array.select_type_items, selectedTypes,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                                selectedTypes[whichButton] = isChecked;
                            }
                        })
                .setPositiveButton(R.string.types_save, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        SharedPreferences.Editor prefEditor = preferences.edit();
                        saveSelectedTypes(prefEditor, availableTypes, selectedTypes);
                    }
                })
                .setNegativeButton(R.string.types_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                })
                .create();
        builder.show();
like image 39
DaJackal Avatar answered Nov 03 '22 20:11

DaJackal