编程方式改变后更新preferences值 [英] update preferences values after changing them programatically

查看:246
本文介绍了编程方式改变后更新preferences值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的preferenceActivity。结果
在我的活动IM使用存储在这些键的值。结果
通过 getDefaultShared preferences getBoolean(somekey)

i've got a simple PreferenceActivity.
in my activity im using the values stored in those keys.
through getDefaultSharedPreferences and getBoolean("somekey")

如果我现在改变为somekey相关联的preference,一切工作正常和结果
在getBoolean返回正确的价值。

if i now change the preference associated with "somekey", everything works fine and
the getBoolean returns the right value.

但如果我设置的值somekey另一个值编程,结果
通过 preferences.Editor 设置视图不会更新到新的价值!
像这样的:

but if i set the value "somekey" to another value programatically,
via Preferences.Editor the settings view doesn't update to the new value! like this:

pref_edit.putBoolean("somekey", true);
pref_edit.commit();

如果somekey是以前真的,我现在得到通过 getBoolean值(somekey); 结果
这是真正喜欢什么我改成了,但是preferences内的复选框错过这一变化。结果
为什么?我怎么可以更新preferences看法?

if "somekey" was true before, and i now get the value via getBoolean("somekey");
it is true like what i changed it to, but the checkbox inside the preferences misses this change.
why? how can i update the preferences view?

推荐答案

好了,有效的问题,它是不容易的 - 有人说完成,其他一些<一个HREF =htt​​p://stackoverflow.com/a/13403122/281545>说

Well, valid question and it's not an easy one - some people say finish, some others say

setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);

和我落得这样做,以避免这样的措施,他们在平板电脑上造成的混乱之类的是注册一个<一个href=\"http://developer.android.com/reference/android/content/Shared$p$pferences.OnShared$p$pferenceChangeListener.html\"相对=nofollow> 共享preferences.OnShared preferenceChangeListener (让我的preferences活动纳入一个是),当我检测在preferences的变化来更新我的复选框(如果它的价值与存储的preference不同意 - 所以它没有手动更改,因为如果手动更改我有一个的onClick 这改变了默认共享preferences)。恼人确实如此。

and what I ended up doing to avoid such measures and the mess they cause on tablets and the like is to register an SharedPreferences.OnSharedPreferenceChangeListener (make my preferences activity into one that is) and when I detect a change in the preferences to update my checkbox (if its value disagrees with the stored preference - so it was not changed manually, cause if changed manually I have an onClick which changes the default shared preferences). Annoying indeed.

这篇关于编程方式改变后更新preferences值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆