[英]How to get the “title” instead of the “value” of a preference with getString()
I'm new to both Java and Android programming and I'm learning fairly quickly, but I can't seem to find any answer to this one after an hour of googling...我是 Java 和 Android 编程的新手,我学得很快,但经过一个小时的谷歌搜索,我似乎找不到任何答案......
I've setup my android app with a preferences.xml that uses a ListPreference entry.我已经使用使用 ListPreference 条目的首选项设置了我的 android 应用程序。xml。 It refers to 2 string arrays, one for the list display, the other for the actual values:指2个字符串arrays,一个为列表显示,另一个为实际值:
<ListPreference
android:title="Draw Rule"
android:summary="Choose between Draw 1 and Draw 3"
android:key="draw_rule"
android:defaultValue="draw3"
android:entries="@array/drawrule"
android:entryValues="@array/drawruleValues" />
My arrays.xml looks like this:我的 arrays.xml 看起来像这样:
<string-array name="drawrule">
<item name="draw1">Draw 1</item>
<item name="draw3">Draw 3</item>
</string-array>
<string-array name="drawruleValues">
<item name="draw1">draw1</item>
<item name="draw3">draw3</item>
</string-array>
To get the preferences, I start by using getDefaultSharedPreferences:为了获得首选项,我首先使用 getDefaultSharedPreferences:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
I understand how to get the correct value from drawruleValues with sharedPrefs.getString("draw_rule", "null")
.我了解如何使用sharedPrefs.getString("draw_rule", "null")
从 drawruleValues 中获取正确的值。 My problem is that for displays purposes I'd like to show the item from drawrule instead, not drawruleValues (eg "Draw Setting: Draw 3"
).我的问题是,出于显示目的,我想显示来自 drawrule 的项目,而不是 drawruleValues (例如"Draw Setting: Draw 3"
)。
Can anyone tell me how to do this?谁能告诉我该怎么做?
Option #1: Don't use separate <string-array>
resources.选项 #1:不要使用单独<string-array>
资源。 Use drawrule
for both arrays in your preference definition.在您的偏好定义中对drawrule
使用拉尺。
Option #2: Iterate over the contents of drawruleValues
(via getStringArray()
on a Resources
object) to find the index of the value, then use that to get the "title" from the drawrule
array (again, via getStringArray()
on a Resources
object).选项 #2: drawruleValues
的内容(通过Resources
对象上的getStringArray()
)以找到该值的索引,然后使用它从drawrule
数组中获取“标题”(再次,通过getStringArray()
上Resources
对象)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.