android - How to show changed SwitchPreference value after AlertDialog click -
i have switchpreference displays alertdialog onchange , accept/reject change depending on button clicked in alertdialog (positive/negative).
if user attempts change value "true" change should accepted, otherwise alertdialog should shown , switchpreference change should rejected. if user confirms alertdialog (positive button) switchpreference should changed.
below relevant part of activity class extends preferenceactivity. values in sharedpreferences updated expected switchpreference not change visibly (switch remains checked) - have re-open settingsactivity see change (switch unchecked).
final switchpreference passwordenabled = (switchpreference) findpreference(password_enabled); passwordenabled.setonpreferencechangelistener(new preference.onpreferencechangelistener() { @override public boolean onpreferencechange(final preference preference, object newvalue) { final context c = getactivity(); if ((boolean) newvalue) { return true; } new alertdialog.builder(c) // omitting non-relevant code .setpositivebutton(getstring(r.string.confirm), new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { ((switchpreference) preference).setchecked(false); preference .geteditor() .putboolean(password_enabled, false) .commit(); preference.setsummary(r.string.disabled); } }) .setnegativebutton(getstring(r.string.cancel), null) .create().show(); return false; } });
add method class contains above mentioned code.
static void updateui() { if(msharedpreferences.getint(msharedprefkey,0) == 0){ log.d(tag,"shared preference stored 0"); mbthcisnooplogserver.setchecked(false); } }
and change switch preference call method:
msharedpreferences.edit().putint(msharedprefkey, 0).commit(); yourclass.updateui();
as method static stored in global content works fine.
Comments
Post a Comment