经过最初的承诺共享preferences.Editor未更新 [英] SharedPreferences.Editor not being updated after initial commit
问题描述
我是有点困惑在这里。我试图改变一个EditText preference的价值,但它不是在视图中更新。 (这是在preferenceActivity)
I'ma a bit confused here. I'm trying to change the value of an EditTextPreference, but it is not updated in the view. (This is in a PreferenceActivity)
下面是我的code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.modify_instrument_preferences);
// Set default values
SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = customSharedPreference.edit();
modifying = getObjectWithName(); //Some object with a name;
editor.putString("namePref", modifying.getName());
editor.commit();
android.util.Log.d("TEST", "written: "+customSharedPreference.getString("namePref",""));
}
我printlns打印出的有效信息,并提交()返回true,但在点击的EditText preference,它显示了旧值。如果我旋转屏幕,导致的onCreate得到再次运行时,EditText上preference有正确的价值。
My printlns print out valid information, and the commit() returns true, but on clicking the EditTextPreference, it displays the old value. If I rotate the screen, causing the onCreate to get run again, the EditTextPreference has the right value.
所以傻了眼。为什么没有这种变化正在对UI更新?
So perplexing. Why isn't this change being updated in the UI?
编辑:
我不知道为什么上面不工作,但我设法只是做来改变它:
I'm not sure why the above isn't working, but I managed to change it just by doing:
EditTextPreference namePref = (EditTextPreference) findPreference("namePref");
namePref.setText("the text");
这是更新视图每次。
推荐答案
虽然我知道有一些结构到位preferenceActivities来跟踪这个信息本身,它似乎并没有被据可查。我发现,添加上preferenceChangeListener
到preference将允许你只要preference改变让那些编辑。
Although I know there are some constructs in place for PreferenceActivities to keep track of this info themselves, it doesn't seem to be well documented. I have found that adding an onPreferenceChangeListener
to the preference will allow you to make those edits as soon as the preference is changed.
这篇关于经过最初的承诺共享preferences.Editor未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!