PreferenceActivity刷新问题
我有从 xml 膨胀的 PreferemceActivity:
<PreferenceScreen
android:title="Appearence"
android:key="AppearencePref" >
......
<PreferenceCategory
android:title="Show Contact Photos">
<CheckBoxPreference
android:title="Show Contact Photos"
android:summary="@string/show_contact_photos_preference"
android:defaultValue="true"
android:key="ShowContactPhotosCheckBoxPref_Appendix" />
</PreferenceCategory>
........
</PreferenceScreen>
.......
<PreferenceScreen
android:title="Contact Options"
android:key="ContactOtionsPref">
<PreferenceCategory
android:title="Show Contact Photos">
<CheckBoxPreference
android:title="Show Contact Photos"
android:defaultValue="true"
android:key="ShowContactPhotosCheckBoxPref" />
</PreferenceCategory>
......
</PreferenceScreen>
其中一个首选项(复选框)更改其他复选框的状态:
if("ShowContactPhotosCheckBoxPref_Appendix".equals(key)){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
boolean isChecked = prefs.getBoolean("ShowContactPhotosCheckBoxPref_Appendix", false);
Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
editor.putBoolean("ShowContactPhotosCheckBoxPref", isChecked);
editor.commit();
}
但是当我使用 ShowContactPhotosCheckBoxPref 进入屏幕时,它仍然保留以前的首选项值... 因此,如果我单击 ShowContactPhotosCheckBoxPref_Appendix - 他的状态现在未选中,然后使用 ShowContactPhotosCheckBoxPref 转到屏幕 - 他的状态仍处于选中状态,但 SharedPreferences 中的值是 false...
我如何告诉 PreferenceActivity 刷新其值?
I have PreferemceActivity inflated from xml:
<PreferenceScreen
android:title="Appearence"
android:key="AppearencePref" >
......
<PreferenceCategory
android:title="Show Contact Photos">
<CheckBoxPreference
android:title="Show Contact Photos"
android:summary="@string/show_contact_photos_preference"
android:defaultValue="true"
android:key="ShowContactPhotosCheckBoxPref_Appendix" />
</PreferenceCategory>
........
</PreferenceScreen>
.......
<PreferenceScreen
android:title="Contact Options"
android:key="ContactOtionsPref">
<PreferenceCategory
android:title="Show Contact Photos">
<CheckBoxPreference
android:title="Show Contact Photos"
android:defaultValue="true"
android:key="ShowContactPhotosCheckBoxPref" />
</PreferenceCategory>
......
</PreferenceScreen>
One of the preferences(checkbox) change state of other checkbox:
if("ShowContactPhotosCheckBoxPref_Appendix".equals(key)){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
boolean isChecked = prefs.getBoolean("ShowContactPhotosCheckBoxPref_Appendix", false);
Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
editor.putBoolean("ShowContactPhotosCheckBoxPref", isChecked);
editor.commit();
}
But when I go to screen with ShowContactPhotosCheckBoxPref it still hold previous preference value...
So if I click on ShowContactPhotosCheckBoxPref_Appendix - his state is now unchecked and then go to screen with ShowContactPhotosCheckBoxPref - his state still checked, but value in SharedPreferences is false...
How can I tell PreferenceActivity to refresh its value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案位于 ApiDemos AdvancedPreferences.java:
Solution is in ApiDemos AdvancedPreferences.java:
您没有更改代码中的值。它应该是:
注意
!
You are not changing the value in your code. It should be:
Notice the
!