How get selected option from ListPreference?

Adam picture Adam · Mar 20, 2012 · Viewed 9.1k times · Source

I have ListPreference and it contains for example 5 options and I want to save one of this value to SharedPreferences when user selects it. How can I do it?

btw. I know how to save value to SharedPreferences, but I don't know how to get that value when user selects one of them.

Answer

Ethan Liou picture Ethan Liou · Mar 20, 2012
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {    
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // newValue is the value you choose
        return true;
    }
};

listPreference.setOnPreferenceChangeListener(listener);