简体   繁体   中英

SharedPreferences how to read title not value from ListPreference

I am reading shared preferences like

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

and then with preferences.getString("list_of_text_modes", "0") i can get value of any shared preferences. In my example (0 or 1)

Is it possible to get title too, not just value?

For example. I am using ListPreference.

<ListPreference
            android:entries="@array/list_of_text_modes"
            android:entryValues="@array/list_of_text_mode_values"
            android:key="list_of_text_modes"
            android:summary=""
            android:title="@string/title_language_mode" 
            android:defaultValue="default" />       

    <string-array name="list_of_text_modes">
        <item>Default</item>
        <item>Settings</item>
    </string-array>
    <string-array name="list_of_text_mode_values">
        <item>0</item>
        <item>1</item>

now i get "0" if i choose "Default". Can i read somehow title "Default". Or with preferences i can read only values?

And what if I don't have 0 and 1. What if i save as "text1" and "tetx2". Can i read by key, value pair?

You can extract titles from your resources, if you have title index. Try this code:

CharSequence[] titles = context.getResources().getTextArray(R.array.list_of_text_modes);
String myTitle = titles[titleIndex];

You can only get the value. If you have a look at the actual shared preferences file which you can pull from the DDMS -> Data - Data -> Package name. You will see only the value and the key is stored and not the title.

But it is not really a problem because you have it already in your array.

Good luck

I was trying to figure this out as well. Too late for the original question, but I came up with a variation on Hit's answer. For arrays where both the title and value are strings it's not simple to find the index.

<string-array name="sound_keys">
    <item>Gong1</item>
    <item>Gong2</item>
</string-array>
<string-array name="sound_values">
    <item>gonghi</item>
    <item>gongmid</item>
</string-array>

But since the value is known you can search the value array and get the index that way, and use that to get the title from it's array. I have a function that does something like this:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String prefValue = sp.getString("sound_values", "some default");
// the arrays used by the ListPreference
CharSequence[] keys = getApplicationContext().getResources().getTextArray(R.array.sound_keys);
CharSequence[] values = getApplicationContext().getResources().getTextArray(R.array.sound_values);
// loop and find index...
int len = values.length;
for (int i = 0; i < len; i++) {
    if (values[i].equals(prefValue)) {
        return (String) keys[i];
    }
}
// if not found use some default value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM