我正在为我的设置屏幕扩展 PreferenceActivity
。在这个偏好活动中,我有几个偏好,其中之一是定制的。问题如下:
在这个自定义首选项(从 ListPreference
扩展)中,我希望能够设置默认值,因此我重写了 setDefaultValue()
方法。在这种方法中,我做了一些解析,因此它将采用正确的值。当我尝试使用 getValue()
函数读取此值时,它仅返回 null
。
所以我想,当我只是在其中放入一些硬编码值时会发生什么(你知道,也许我做错了什么,这不是第一次)。好吧,我仍然得到 null
回来。
有什么想法我做错了吗?
编辑:
在 xml 文件中设置 defaultValue 实际上并不是一个选项,因为在我检索这些值之前,这些值是未知的。
我做了一个解决方法:
- 当应用程序第一次启动时:获取数据
- 在首选项中设置值。
这样我在收集数据时设置默认首选项
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:
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
.
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?
Edit:
Setting the defaultValue in the xml file isn't really an option because the values aren't known until i retrieve them.
I made a workaround:
- When app is started for the first time: get data
- Set the values in the preference.
This way i set the default preference when i'm collection the data
发布评论
评论(7)
我终于找到了解决方案(除了StackOverflow之外,这一次) 。
创建自定义 Preference 类时,
onSetInitialValue
实现为 XåpplI'-I0llwlg'I - 指出例如,如果自定义首选项保存为 int,
现在
PreferenceManager.setDefaultValues()
finally 也会加载自定义首选项的android:defaultValue
。仍然没有解决 null 和 false 的问题,但是对于其他地方发布的问题有一些解决方法。I finally found the solution (somewhere besides StackOverflow, for once).
When you create a custom Preference class,
onSetInitialValue
as XåpplI'-I0llwlg'I - pointed outonGetDefaultValue(TypedArray a, int index)
For example, if the custom preference is saved as an int,
Now
PreferenceManager.setDefaultValues()
finally loads theandroid:defaultValue
for the custom preferences too. Still no fix for nulls and false, but there are workarounds for those posted elsewhere.如果您想在调用
setDefaultValue()
后调用getValue()
以在 PreferenceActivity 首次打开时检索默认值,则需要重写onSetInitialValue()< /code> 在您的 Preference 子类中。否则,当您调用
getValue()
时,将不会设置默认值,并且它将返回null
(如您所经历的)。例如,如果您的默认值是整数,则
onSetInitialValue()
可能如下所示:DEFAULT_VALUE
只是 Preference 内的一个私有常量,用于在持久化时使用int 无法检索。setValue()
是公共 setter,用于补充getValue()
公共 getter,并且应如下所示:有关
onSetInitialValue()
的更多信息>,参考API文档 这里。查看 Preference 类的源代码也是一个好主意(这里)来理解为什么需要实现
onSetInitialValue()
。特别是,请查看setDefaultValue()
,然后查看dispatchSetInitialValue()
。If you want to call
getValue()
after callingsetDefaultValue()
to retrieve a default value the first time your PreferenceActivity opens, you need to overrideonSetInitialValue()
in your Preference subclass. Otherwise, the default value will not be set when you callgetValue()
and it will return anull
(as you experienced).For example, if your default value is an integer, your
onSetInitialValue()
might look like this:DEFAULT_VALUE
is just a private constant inside the Preference to be used in case the persisted int cannot be retrieved.setValue()
is the public setter to complement yourgetValue()
public getter, and should look something like this:For more information about
onSetInitialValue()
, refer to the API documentation here.It's also a good idea to look at the source code of the Preference class (here) to understand why
onSetInitialValue()
needs to be implemented. In particular, have a look atsetDefaultValue()
, and then look atdispatchSetInitialValue()
.setDefaultValue
并不像您想象的那样工作。看看 Preference.java 的源代码,您将了解其背后的逻辑。设置默认值的首选方法是在应用的
preferences.xml
文件中指定android:defaultValue
属性。setDefaultValue
doesn't work the way you think it does. Look at the source ofPreference.java
and you'll the logic behind it all.The preferred way to set a default is to specify the
android:defaultValue
attribute in thepreferences.xml
file of your app.我将首选项
.xml
转换为代码。所有setDefaultValue
都可以正常工作。更多信息
PS:我发现这种方式更小更清晰而不是自定义所有首选项或其他答案。
I converted preferences
.xml
to code. AllsetDefaultValue
s works well there.more info
P.S: I found this way more smaller and clear than customizing all preferences or other answers.
您可以扩展首选项并在构建过程中设置默认值,如下所示:
然后您可以像这样从 XML 使用它:
You can extend preference and set the default value during constructing like this:
then you can use it from XML like this:
这就是我所做的并为我工作:
This is what I did and worked for me:
我认为这在任何时候都有效。
此代码将检测对话框何时即将启动,并使用默认值填充对话框中的 EditText 或 List。
I think this works too at anytime.
This code will detect when the dialog is about to launch and populate the EditText or List in the dialog with your default value.