0

I have a listView activity and need to apply and save some preferences made by user. Each row of my ListView contains of two TextViews (year and country).

I handle onSharedPreferenceChanged event this way:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.
        OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        TextView year = (TextView) findViewById(R.id.year);
        year.setTextSize(Float.parseFloat(prefs.getString(key, null)));
        TextView country = (TextView) findViewById(R.id.country);
        country.setTextSize(Float.parseFloat(prefs.getString(key, null)));
    }
});

But the changes are applied only for the first row, the others stay the same as before.

What should I do to apply new settings for each row of the list? Thanks for your answers.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Dmitry
  • 85
  • 10

1 Answers1

0

Change your code to :

OnSharedPreferenceChangeListener listener; // class field

// in onResume()
SharedPreferences prefs = PreferenceManager
        .getDefaultSharedPreferences(this);
listener = new SharedPreferences.
        OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {
        TextView year = (TextView) findViewById(R.id.year);
        year.setTextSize(Float.parseFloat(prefs.getString(key, null)));
        TextView country = (TextView) findViewById(R.id.country);
        country.setTextSize(Float.parseFloat(prefs.getString(key, null)));
    }
};
prefs.registerOnSharedPreferenceChangeListener(listener);
// unregister in onPause()

For an explanation see here

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361