获取密钥,并不看重列表preference选择, - 可能吗? [英] Get Key, not value, of the ListPreference selection - Possible?
问题描述
获取的值的在目录preference
当前选择的项目是简单的:
Getting the value of the currently selected item in a ListPreference
is straightforward:
String selected = sharedPrefs.getString(
getString(R.string.list_preference_array),
"default string"
);
但现在我需要获得当前所选项目的键代替。这可能吗?
要澄清一下,在XML文件中的典型列表preference定义包括以下组件:
To clarify, a typical ListPreference definition in the XML file has the following components:
<ListPreference
android:key="@string/list_preference_array"
android:title="Title of ENTIRE list (not seen by user?)"
android:summary="this is what the user sees in small fonts"
android:defaultValue="just in case"
android:entries="@array/user_friendly_labels"
android:entryValues="@array/code_meaningful_strings"
android:dialogTitle="User Prompt(big font)"
android:showDefault="true"
android:showSilent="true"
/>
什么共享prefs.getString()
收益为 Android的当前选择:entryValues 。我所感兴趣的是得到从 Android的当前选择:项即可。我错误地把它称为钥匙,但实际上它是一个对应的标签,它必须比实际内容不同。
What sharedPrefs.getString()
returns is the current selection from android:entryValues. What I am interested in getting is the current selection from android:entries. I mistakenly called it "key" but really it is a "corresponding label", which must be different than actual content.
推荐答案
猜测的一点:
int index = mylistpreference.findIndexOfValue(selected) // <- selected taken from your code above
String entry = mylistpreference.getEntries()[index];
这篇关于获取密钥,并不看重列表preference选择, - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!