我有一个listView活动,需要应用并保存用户做出的一些偏好。 我的ListView的每一行包含两个TextView(年份和国家)。
我以这种方式处理onSharedPreferenceChanged事件:
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)));
}
});
但是更改仅适用于第一行,其他更改与之前保持一致。
如何为列表的每一行应用新设置?谢谢你的回答。
答案 0 :(得分:0)
将您的代码更改为:
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()
有关说明,请参阅here