Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i obtain the setMultiChoiceItems items from my AlertDialog?

Tags:

android

I'm using the following code from the apidemos..

        return new AlertDialog.Builder(AlertDialogSamples.this)
            .setIcon(R.drawable.ic_popup_reminder)
            .setTitle(R.string.alert_dialog_multi_choice)
            .setMultiChoiceItems(R.array.select_dialog_items3,
                    new boolean[]{false, true, false, true, false, false, false},
                    new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton,
                                boolean isChecked) {

                            /* User clicked on a check box do some stuff */
                        }
                    })
            .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            })
           .create();

Under /* User clicked Yes so do some stuff */, i'm not fed the isChecked variable.. however, I am given isChecked in the /* User clicked on a check box do some stuff */ section.. Obviously i don't want to update my SharedPrefs on checkbox click, in case the user hits cancel..

So how do I get the checkboxs and values on the setPositiveButton onClick ?

Thanks.

like image 318
eportermd Avatar asked Nov 12 '10 08:11

eportermd


People also ask

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

What is the significance of AlertDialog builder?

Android AlertDialog is one of the most important and basic component in Android applications. Alert dialog box is used to show alerts to the users, get confirmation from the users. In order to make an alert dialog, we need to make an object of AlertDialogBuilder which is an inner class of AlertDialog .


1 Answers

use getListView() of AlertDialog class. and fetch the listView. i.e when you call .create() at the end this will fetch you a new AlertDialog.

Then use one of the following methods:

 1. getCheckItemPositions:SparseBooleanArray
 2. getCheckedItemIds:long[]

I can provide you with demo code. Give me 10-15 mins.

like image 142
Shardul Avatar answered Sep 29 '22 19:09

Shardul