Example usage for android.preference MultiSelectListPreference findIndexOfValue

List of usage examples for android.preference MultiSelectListPreference findIndexOfValue

Introduction

In this page you can find the example usage for android.preference MultiSelectListPreference findIndexOfValue.

Prototype

public int findIndexOfValue(String value) 

Source Link

Document

Returns the index of the given value (in the entry values array).

Usage

From source file:net.sf.sprockets.app.ui.SprocketsPreferenceFragment.java

/**
 * Set the preference's value(s) as its summary.
 *//*from   w ww.  j  a  va2 s.co  m*/
protected void setSummary(Preference pref) {
    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    if (pref instanceof RingtonePreference) {
        String val = prefs.getString(pref.getKey(), null);
        if (!TextUtils.isEmpty(val)) {
            Ringtone tone = RingtoneManager.getRingtone(a, Uri.parse(val));
            if (tone != null) {
                pref.setSummary(tone.getTitle(a));
            }
        } else {
            pref.setSummary(R.string.none);
        }
    } else if (pref instanceof MultiSelectListPreference) {
        Set<String> vals = prefs.getStringSet(pref.getKey(), null);
        if (vals != null) {
            if (!vals.isEmpty()) {
                MultiSelectListPreference multi = (MultiSelectListPreference) pref;
                IntList indexList = new ArrayIntList(vals.size());
                for (String val : vals) { // find selected entry indexes
                    int i = multi.findIndexOfValue(val);
                    if (i >= 0) {
                        indexList.add(i);
                    }
                }
                int[] indexes = indexList.toArray();
                Arrays.sort(indexes); // to display in same order as dialog
                pref.setSummary(TextUtils.join(getString(R.string.delimiter),
                        Elements.slice(multi.getEntries(), indexes)));
            } else {
                pref.setSummary(R.string.none);
            }
        }
    } else if (pref instanceof EditTextPreference) {
        pref.setSummary(prefs.getString(pref.getKey(), null));
    } else if (pref.getClass() == Preference.class) {
        String val = prefs.getString(pref.getKey(), null);
        if (!TextUtils.isEmpty(val)) { // don't clear existing summary
            pref.setSummary(val);
        }
    }
}