我是Java和Android编程的新手,而且我的学习速度相当快,但是经过一小时的谷歌搜索,我似乎无法找到任何答案......
我使用一个使用ListPreference条目的preferences.xml设置我的Android应用程序。它指的是2个字符串数组,一个用于列表显示,另一个用于实际值:
<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" />
我的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>
要获得首选项,我首先使用getDefaultSharedPreferences:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
我了解如何使用sharedPrefs.getString("draw_rule", "null")
从drawruleValues中获取正确的值。我的问题是,出于显示目的,我想要显示来自drawrule的项目,而不是drawruleValues(例如"Draw Setting: Draw 3"
)。
谁能告诉我怎么做?
答案 0 :(得分:1)
选项#1:不要使用单独的<string-array>
资源。在您的首选项定义中对两个数组使用drawrule
。
选项#2:迭代drawruleValues
的内容(通过getStringArray()
对象上的Resources
)来查找值的索引,然后使用它来获取“标题”来自drawrule
数组(再次通过getStringArray()
对象上的Resources
。