调用setDefaultValue()上的自定义preference类不设置默认值。为什么? [英] Calling setDefaultValue() on a custom Preference class does not set the default value. Why?
问题描述
我延长 preferenceActivity
我的设置屏幕。在这个preference活动我有一对夫妇preferences,其中一个是定制。的问题如下:
I'm extending PreferenceActivity
for my settings screen. In this preference activity i have a couple of preferences one of which is custom made. The problem is as follows:
在这个自定义preference我希望能够设置默认值(从目录preference
扩展),所以我重写 setDefaultValue()
方法。在这种方法中我做了一些分析,以便它会采取正确的值。当我试图读取它只是返回空
的的getValue()
函数这个值。
in this custom preference (which extends from ListPreference
) i want to be able to set the default value, so i override the setDefaultValue()
method. In this method i do some parsing so it'll take the correct value. When i'm trying to read this value with the getValue()
function it just returns null
.
所以我想,如果我只是摆在那里的一些硬codeD值(你知道,也许我做错了什么,不会是第一次)会发生什么。好了,我仍然得到空
回来了。
So i figured, what happens when i just put some hardcoded value in there (you know, maybe i did something wrong, wouldn't be the first time). Well, i still get null
back.
任何想法,我做错了什么?
Any ideas what i'm doing wrong?
编辑:结果
在XML文件中设置将默认值是不是真的,因为数值是不知道,直到我找回它们的选项。
Setting the defaultValue in the xml file isn't really an option because the values aren't known until i retrieve them.
我做了一个解决方法:
- 当应用程序启动的第一次:获取数据
- 在preference设置的值。
这种方式,我设置的默认preference当我收集的数据
This way i set the default preference when i'm collection the data
推荐答案
setDefaultValue
不工作,你觉得它的方式。看<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r1.2/android/$p$pference/$p$pference.java#$p$pference\"相对=nofollow> preference.java
>,你会这一切背后的逻辑。
setDefaultValue
doesn't work the way you think it does. Look at the source of Preference.java
and you'll the logic behind it all.
在preferred的方式来设置默认是指定的android:设置defaultValue
的属性 preferences.xml
文件您的应用程序。
The preferred way to set a default is to specify the android:defaultValue
attribute in the preferences.xml
file of your app.
这篇关于调用setDefaultValue()上的自定义preference类不设置默认值。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!