How to Get Selected Text and Value Android ListPreference

33,153

Solution 1

in your PreferenceActivity do something like:

ListPreference listPreference = (ListPreference) findPreference("lpBirim");
CharSequence currText = listPreference.getEntry();
String currValue = listPreference.getValue();

Solution 2

You can use this snippet to get the value:

 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
 sp.getString("lpBirim","-1")

Have look on the tutorial

Solution 3

Here is an example:

@Override
public boolean onPreferenceChange(Preference preference, Object value)
{
    String textValue = value.toString();

    ListPreference listPreference = (ListPreference) preference;
    int index = listPreference.findIndexOfValue(textValue);

    CharSequence[] entries = listPreference.getEntries();

    if(index >= 0)
        Toast.makeText(preference.getContext(), entries[index], Toast.LENGTH_LONG);

    return true;
}
  • index contains the index of the clicked item
  • textValue is the Selected Value
  • entries[index] is the Selected Text

Solution 4

SharedPreferences Preference = PreferenceManager.getDefaultSharedPreferences(this); 
 Preference.getString("your list preference key","-1")
Share:
33,153
david
Author by

david

Updated on July 09, 2022

Comments

  • david
    david almost 2 years

    The XML file of my ListPreference

    <ListPreference android:key="lpBirim" android:title="Birim"
            android:summary="" android:defaultValue="0"  android:persistent="false"/>
    

    How to get the selected text and the selected value?

  • david
    david about 13 years
    I get Selected value From this function newValue Field But How To Get Selected Text? public boolean onPreferenceChange(Preference preference, Object newValue)