获取密钥,并不看重列表preference选择, - 可能吗? [英] Get Key, not value, of the ListPreference selection - Possible?

查看:125
本文介绍了获取密钥,并不看重列表preference选择, - 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取的的在目录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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆