PreferenceActivity刷新问题

发布于 2024-11-01 03:41:43 字数 1959 浏览 6 评论 0原文

我有从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-11-08 03:41:43

解决方案位于 ApiDemos AdvancedPreferences.java

private CheckBoxPreference mCheckBoxPreference;

mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
        KEY_ADVANCED_CHECKBOX_PREFERENCE);

if (mCheckBoxPreference != null) {
    mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
}

Solution is in ApiDemos AdvancedPreferences.java:

private CheckBoxPreference mCheckBoxPreference;

mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
        KEY_ADVANCED_CHECKBOX_PREFERENCE);

if (mCheckBoxPreference != null) {
    mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
}
一张白纸 2024-11-08 03:41:43

您没有更改代码中的值。它应该是:

editor.putBoolean("ShowContactPhotosCheckBoxPref", !isChecked);

注意 !

You are not changing the value in your code. It should be:

editor.putBoolean("ShowContactPhotosCheckBoxPref", !isChecked);

Notice the !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文