Reputation: 6794
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.
Upvotes: 4
Views: 8948
Reputation: 1011
To properly update the summary of a ListPreference (using the label, instead of the key).
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
CharSequence[] labels = listPreference.getEntries();
preference.setSummary(labels[prefIndex]);
}
} else {
preference.setSummary(stringValue);
}
return true;
}
Upvotes: 0
Reputation: 419
@shai Your method lags to last selected entry
listPreference.getEntry()
Rather @Erik 's method works nicely
int index = listPreference.findIndexOfValue((String) value)
String entry = listpreference.getEntries()[index];
Upvotes: 0
Reputation: 835
just use:
mylistpreference.getEntry()
to get mylistpreference use:
mylistpreference= (ListPreference) getPreferenceScreen().findPreference(key);
key is the android:key you defined in preference.xml inside < ListPreference> tag.
Upvotes: 1
Reputation: 1694
A bit of a guess:
int index = mylistpreference.findIndexOfValue(selected) // <- selected taken from your code above
String entry = mylistpreference.getEntries()[index];
Upvotes: 14
Reputation: 22240
The function is defined as
SharedPreferences.getString(String key, String defaultValue);
So in your example code, getString(R.string.select_string)
would return the key.
When you add the SharedPreference
, you need to specify the key, so the key would be the same one you used to set the value.
Edit:
using SharedPreferences.getString()
with the ListPreference key as the key will return the value the user selected from the list. You don't need to create keys for each option in the ListPreference array, and these keys aren't created automatically. Use case statements that correlate to the ListPreference's entryValues
.
Upvotes: 0