Change the summary of a ListPreference with the new value (Android)

前端 未结 14 987
感动是毒
感动是毒 2020-12-30 03:01

How can I modify the summary of a ListPreference to the new \"Entry\" string selected by the user (not the entry value)

I suppouse its with setOnPreferenceChangeList

14条回答
  •  别那么骄傲
    2020-12-30 03:52

    i solved this problem with another and simple solution (https://gist.github.com/brunomateus/5617025):

    public class ListPreferenceWithSummary extends ListPreference{
    
    public ListPreferenceWithSummary(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public ListPreferenceWithSummary(Context context) {
        super(context);
    }
    
    @Override
    public void setValue(String value) {
        super.setValue(value);
        setSummary(value);
    }
    
    @Override
    public void setSummary(CharSequence summary) {
        super.setSummary(getEntry());
    }
    }
    

    This worked very fine on my GirgerBeard device. Even when is the first time running your app. Don't forget to provide default value on your xml prefence:

    android:defaultValue="default value"
    

    and set default values on your PreferenceActivity or PrefenceFragment:

     PreferenceManager.setDefaultValues(this, R.xml.you pref file, false);
    

提交回复
热议问题