从XML preference文件设置defaultValue不存储 - 为什么? [英] defaultValue from xml preference file isn't stored - why?

查看:128
本文介绍了从XML preference文件设置defaultValue不存储 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序第一次startet,我想用'机器人:设置defaultValue来存储我在我的prefences.xml定义的所有默认值属性,但其中有些是不存储在设备 - ?谁能告诉我,为什么

When the application is first startet, I'd like to store all default values I've defined in my prefences.xml by using the 'android:defaultValue' attribute, but some of them are not stored on the device - can someone tell me why?

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory android:title="@string/prefs_cat_title_x">
    <ListPreference
        android:key="@string/prefs_key_1"
        android:title="@string/prefs_title_1"
        android:summary="@string/prefs_summary_1"
        android:entries="@array/array1"
        android:entryValues="@array/array1"
        android:defaultValue="@string/prefs_default_1"/>
    <com.myapp.TimePreference
        android:key="@string/prefs_key_2"
        android:title="@string/prefs_title_2"
        android:defaultValue="@string/prefs_default_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <com.myapp.TimePreference
        android:key="@string/prefs_key_3"
        android:title="@string/prefs_title_3"
        android:defaultValue="@string/prefs_default_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ListPreference
        android:key="@string/prefs_key_4"
        android:title="@string/prefs_title_4"
        android:summary="@string/prefs_summary_4"
        android:entries="@array/array2"
        android:entryValues="@array/array2"
        android:defaultValue="@string/prefs_default_4"/>
    <CheckBoxPreference
        android:key="@string/prefs_key_5"
        android:title="@string/prefs_title_5"
        android:summary="@string/prefs_summary_5"
        android:defaultValue="false"/>
    <CheckBoxPreference
        android:key="@string/prefs_key_6"
        android:title="@string/prefs_title_6"
        android:summary="@string/prefs_summary_6"
        android:defaultValue="false"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/prefs_cat_title_common">
    <com.myapp.DatabaseResetPreference
        android:title="@string/prefs_title_7"
        android:summary="@string/prefs_summary_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</PreferenceCategory>    

推荐答案

我已经找到了解决我的问题,但它仍然没有回答我的问题。
我不得不改变路线:

I've found a solution to my problem, but it still doesn't answer my question. I had to change the line:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

PreferenceManager.setDefaultValues(this, R.xml.preferences, true);

由于文档说,设置readAgain应该不会覆盖任何现有preference值:

As the docs say, setting readAgain should not overwrite any existing preference values:

请注意:这不会重置preferences
  回到它们的默认值。

"Note: this will NOT reset preferences back to their default values."

只需用真为我工作,但我仍然不知道为什么只对我的三个preferences的默认设置使用假时,即使包含KEY_HAS_SET_DEFAULT_VALUES xml文件不存在在设备上(因此没有被设置为true)(它的存在直到我叫上面的方法)。

Simply using "true" works for me, but I still don't know why only the defaults for three of my preferences are set when using "false", even though the xml file containing KEY_HAS_SET_DEFAULT_VALUES didn't exist (and so wasn't set to true) on the device (it existed not until I called the method above).

如果任何人知道该行为的一个可能的原因,请让我知道!

If anyone knows a possible reason for that behavior, please let me know!

这篇关于从XML preference文件设置defaultValue不存储 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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