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"
);
But now I need to get the key of the currently selected item, instead. Is this possible?
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"
/>
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.
A bit of a guess:
int index = mylistpreference.findIndexOfValue(selected) // <- selected taken from your code above
String entry = mylistpreference.getEntries()[index];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With