【问题标题】:Why are my Android Preferences not updating?为什么我的 Android 偏好设置没有更新?
【发布时间】:2014-05-02 14:39:39
【问题描述】:

我在一个 xml 文件中设置了一些首选项,并使用标准结构(参见下面的代码)使它们可用且可更新,并且一切正常。由于这些是出于测试目的,我现在想用我的实际应用偏好(包括完全删除一些)替换它们,但无论我做什么,它似乎都只能识别我设置的原始偏好。我什至用我的新文件完全替换了 xml 文件——它仍然只找到旧的首选项。我还清除了代码中显示的条目(在 commit() 之后地图大小为 0)。此外,它似乎不允许我从“preferences.xml”更改 xml 文件名。代码显示了我的新名称 preferences_new.xml 但这会在 Eclipse 中引发错误。

在调用活动 onCreate:

        // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new PrefsFragment()).commit();

在 PrefsFragment 中:

        PreferenceManager pm = this.getPreferenceManager();
    SharedPreferences sp = pm.getSharedPreferences();
    Editor e = sp.edit();
    e.clear();
    e.commit();
    Map <String, ?> map = sp.getAll();
    int size = map.size();
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences_new);

旧 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" >
    <EditTextPreference
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:key="edittext_preference"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />

    <ListPreference
        android:dialogTitle="@string/dialog_title_list_preference"
        android:entries="@array/entries_list_preference"
        android:entryValues="@array/entryvalues_list_preference"
        android:key="list_preference"
        android:summary="@string/summary_list_preference"
        android:title="@string/title_list_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/launch_preferences" >

    <!--
         This PreferenceScreen tag serves as a screen break (similar to page break
         in word processing). Like for other preference types, we assign a key
         here so it is able to save and restore its instance state.
    -->
    <PreferenceScreen
        android:key="screen_preference"
        android:summary="@string/summary_screen_preference"
        android:title="@string/title_screen_preference" >

        <!-- You can place more preferences here that will be shown on the next screen. -->

        <CheckBoxPreference
            android:key="next_screen_checkbox_preference"
            android:summary="@string/summary_next_screen_toggle_preference"
            android:title="@string/title_next_screen_toggle_preference" />
    </PreferenceScreen>
    <PreferenceScreen
        android:summary="@string/summary_intent_preference"
        android:title="@string/title_intent_preference" >
        <intent
            android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
    </PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/preference_attributes" >
    <CheckBoxPreference
        android:key="parent_checkbox_preference"
        android:summary="@string/summary_parent_preference"
        android:title="@string/title_parent_preference" />

    <!-- The visual style of a child is defined by this styled theme attribute. -->
    <CheckBoxPreference
        android:dependency="parent_checkbox_preference"
        android:key="child_checkbox_preference"
        android:layout="?android:attr/preferenceLayoutChild"
        android:summary="@string/summary_child_preference"
        android:title="@string/title_child_preference" />
</PreferenceCategory>

新建 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" 
                android:key="edittext_category" >
    <EditTextPreference
        android:id="@+id/my_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:defaultValue=""
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:inputType="phone"
        android:key="edittext_preference"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />
</PreferenceCategory>

我几乎处于“扯掉头发”阶段,因此我们将不胜感激任何帮助

【问题讨论】:

  • 等等,保存首选项的 XML 与您的首选项片段(显示的布局)不同。
  • 嗨,马可。你能澄清你的意思吗。谢谢
  • 您想从 XML 布局文件中删除所有首选项吗? (R.xml.preferences_new) -- 我的意思是:R.xml.preferences_new 是一个东西,SharedPreferences 是另一个东西。您的 XML (R.xml.preferences_new) 不是保存 SharedPreference 的地方。它是您的应用数据中的另一个 XML。所以当你做 e.clear();您只需删除此“私有”-shared-preference-xml 文件中的所有首选项,而不是 R.xml.preferences_new
  • 好的,我想我明白了,但我添加了 clear() 等以尝试清除旧的首选项并替换为新的(即旧 xml 下列出的那些仍然出现在我的首选项中UI 屏幕,即使我已将其替换为新的 xml。)也许我误解了整个过程。如果问题不清楚,我很抱歉。目前这一切都让我感到非常困惑。我需要的是两个持久的偏好字段:一个复选框和一个编辑文本,并摆脱所有这些其他字段!

标签: java android xml sharedpreferences


【解决方案1】:

我现在认为我无法替换首选项文件的原因是 R.java 由于某种难以解决的原因没有重新生成。我放弃了调查(在here 之后)并返回到我以前的工作版本并重新应用更改,直到在偏好工作之前让我的应用程序恢复工作(显示保留代码版本的重要性)。这对我来说是一个更快的选择。

我现在以不同的方式实现了我的首选项(使用示例here)并放弃了使用PreferenceFragment。所以,感谢 Marco 让我保持清醒。我现在可以继续前进了。

【讨论】:

    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多