mardi 30 juin 2015

Checkbox status not being passed when dialog button pressed

I'm trying to implement a check box in a MaterialDialog using this library, and the check box asks the user if they don't want to see that dialog again. The dialog appears if the user's phone has NFC, but it is deactivated.

If the user presses the positive button in the dialog and has the box ticked, then it accesses a Realm object with a Boolean attribute named "NfcStatus", and sets that to true. If they press the negative button with the box ticked, then that Realm object's NfcStatus is set to false.

Here's the code of the MaterialDialog:

new MaterialDialog.Builder(context)
            .title("NFC")
            .content("NFC is disabled. Would you like to activate it?")
            .items(R.array.checkbox) //this just has one string in it which says "Please don't show me this again"
            .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
                @Override
                public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
                    /**
                     * If you use alwaysCallMultiChoiceCallback(), which is discussed below,
                     * returning false here won't allow the newly selected check box to actually be selected.
                     * See the limited multi choice dialog example in the sample project for details.
                     **/
                    checkboxIsChecked = true; //TODO: checkboxIsChecked isn't being passed into onPositive or onNegative
                    return true;
                }
            })
            .positiveText(R.string.accept)
            .positiveColorRes(R.color.main_theme_color)
            .negativeText(R.string.decline)
            .negativeColorRes(R.color.main_theme_color)
            .callback(new MaterialDialog.ButtonCallback() {
                @Override
                public void onPositive(MaterialDialog dialog) {
                    //this was how I was checking if checkboxIsChecked was true or false
                    Log.d("checkboxIsChecked", checkboxIsChecked?"true":"false"); }

                    if (checkboxIsChecked) {begins
                        if (ks.contains(KEY_NAME)) {
                            realmKey = ks.get(KEY_NAME);
                        }
                        realm = Realm.getInstance(context, realmKey);
                        RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();                      realmPhone.setNfcStatus(true);                          
                    }
                    activity.finish();
                    startNfcSettingsActivity();

                    Toast.makeText(context, R.string.nfc_disabled_message, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onNegative(MaterialDialog dialog) {
                    if (checkboxIsChecked) {
                        if (ks.contains(KEY_NAME)) {
                            realmKey = ks.get(KEY_NAME);
                        }
                        realm = Realm.getInstance(context, realmKey);
                        RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();
                        realmPhone.setNfcStatus(false);

                    }
                }

            })
            .cancelable(false)
            .show();

The problem was that even if the check box was ticked, the checkboxIsChecked variable was still false when using it in onPositive or onNegative, so it was never being written to the Realm object. Am I doing this the wrong way?




Aucun commentaire:

Enregistrer un commentaire