Update EditText preferences depending on changes in another Preferences

I want to change the values displayed in each Preferencein my PreferenceActivitybased on a change in another preference.

I have an application that stores metric (float) values ​​in Preferences. I also have Preference, called metric, where the user can switch between using metric or imperial units.

I save only metric values, but when the user switches to imperial, the application displays imperial values.

Everything works fine if the user only switches the metric preference and leaves PreferenceActivity. When he returns, the values ​​are displayed correctly. If he looks at userHeight, for example, the display still shows a metric value.

In mine, PreferenceActivityI want to update the displayed values ​​when the metricPreference changes (I'm sure that when these values ​​are saved, they will be returned correctly):

this.metric.setOnPreferenceChangeListener (new OnPreferenceChangeListener () {
    @Override
    public boolean onPreferenceChange (Preference preference, Object newValue) {
        final Boolean metric = Boolean.parseBoolean (newValue.toString ());
        changeSummaries (metric);
        refreshDisplay (metric); 
        return true;
    }
});

refreshDisplay(...)? , PreferenceActivity ?

+3
1

. , .

EditTextPreference, - String

, , onBindDialogView Preference ( , - ). onBindDialogView , :

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);
    getEditText().setText(getPersistedString(""));
}

@Override
protected String getPersistedString(String defaultReturnValue) {
    final Float storedValue = getPersistedFloat(-1);
    Float returnValue;
    if (MyPreferences.isMetric(getContext())) {
        returnValue = storedValue;
    } else {
        returnValue = this.unit.convertToImperial(storedValue);
    }
    return String.valueOf(returnValue);
}
+2

Source: https://habr.com/ru/post/1796004/


All Articles