2

I have a subclass of DialogPreference called MyDialogPreference, it has two EditTexts for username and password, when I click in the DialogInterface.BUTTON_POSITIVEbutton, I set some preferences like this:

//I get userId from SQLite before this
SharedPreferences settings = this.getContext()
                .getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();

editor.putInt("userId", userId);
editor.commit();

I want that in my SettingsFragment that called the DialogPreference, whenever I close MyDialogPreference, I could be able to show the data changed as a summary of the Preference

Here's the layour of Preferences:

    <Preference
        android:key="update"
        android:title="Update other preference" >
    </Preference>

    <com.cmr.MyDialogPreference
        android:key="userPassScreen"
        android:title="Login User" >
    </com.cmr.MyDialogPreference>

I was trying to bind this DialogPreference to OnPreferenceChangeListener, but it didnt work.

Here's the code for that also:

DialogPreference userPassScreen = (DialogPreference) findPreference("userPassScreen");
userPassScreen
            .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    SharedPreferences settings = context
                            .getSharedPreferences(PREFS_NAME, 0);

                    int userId = settings.getInt("userId", 0);
                    if (userId == 0) {
                        preference.setSummary("Please Login");
                        return false;
                    }
                    try {
                        preference.setSummary(String.valueOf(userId));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return false;
                }
            });

Is there any way I can make this?

PS. Sorry for my bad english

jmacboy
  • 323
  • 3
  • 16
  • well, nobody answered, but I currently found a solution to this question [here](http://stackoverflow.com/a/531927/315905), using SharedPreferencesListener – jmacboy Aug 30 '13 at 19:40

1 Answers1

4

The Android documentation of DialogPreference is incomplete!

Just take a look at the JavaDoc of Preference#onClick()

/**
 * Processes a click on the preference. This includes saving the value to
 * the {@link SharedPreferences}. However, the overridden method should
 * call {@link #callChangeListener(Object)} to make sure the client wants to
 * update the preference's state with the new value.
 */
protected void onClick() {
}

"the overridden method should call {@link #callChangeListener(Object)}"

In DialogPreference onClick is overridden to open the dialog, but the JavaDoc of onDialogClosed() does not mention to call #callChangeListener(Object).

So just add a

callChangeListener(newValue);

call to the beginning of your onDialogClosed() implementation.

However this should have been implemented in DialogPreference itself.

pentike
  • 418
  • 4
  • 14