FileDocCategorySizeDatePackage
MultiSelectListPreference.javaAPI DocAndroid 5.1 API9520Thu Mar 12 22:22:10 GMT 2015android.preference

MultiSelectListPreference

public class MultiSelectListPreference extends DialogPreference
A {@link Preference} that displays a list of entries as a dialog.

This preference will store a set of strings into the SharedPreferences. This set will contain one or more values from the {@link #setEntryValues(CharSequence[])} array.

attr
ref android.R.styleable#MultiSelectListPreference_entries
attr
ref android.R.styleable#MultiSelectListPreference_entryValues

Fields Summary
private CharSequence[]
mEntries
private CharSequence[]
mEntryValues
private Set
mValues
private Set
mNewValues
private boolean
mPreferenceChanged
Constructors Summary
public MultiSelectListPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


     
                    
        super(context, attrs, defStyleAttr, defStyleRes);

        final TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.MultiSelectListPreference, defStyleAttr,
                defStyleRes);
        mEntries = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entries);
        mEntryValues = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entryValues);
        a.recycle();
    
public MultiSelectListPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        this(context, attrs, defStyleAttr, 0);
    
public MultiSelectListPreference(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
    
public MultiSelectListPreference(android.content.Context context)

        this(context, null);
    
Methods Summary
public intfindIndexOfValue(java.lang.String value)
Returns the index of the given value (in the entry values array).

param
value The value whose index should be returned.
return
The index of the value, or -1 if not found.

        if (value != null && mEntryValues != null) {
            for (int i = mEntryValues.length - 1; i >= 0; i--) {
                if (mEntryValues[i].equals(value)) {
                    return i;
                }
            }
        }
        return -1;
    
public java.lang.CharSequence[]getEntries()
The list of entries to be shown in the list in subsequent dialogs.

return
The list as an array.

        return mEntries;
    
public java.lang.CharSequence[]getEntryValues()
Returns the array of values to be saved for the preference.

return
The array of values.

        return mEntryValues;
    
private boolean[]getSelectedItems()

        final CharSequence[] entries = mEntryValues;
        final int entryCount = entries.length;
        final Set<String> values = mValues;
        boolean[] result = new boolean[entryCount];
        
        for (int i = 0; i < entryCount; i++) {
            result[i] = values.contains(entries[i].toString());
        }
        
        return result;
    
public java.util.SetgetValues()
Retrieves the current value of the key.

        return mValues;
    
protected voidonDialogClosed(boolean positiveResult)

        super.onDialogClosed(positiveResult);
        
        if (positiveResult && mPreferenceChanged) {
            final Set<String> values = mNewValues;
            if (callChangeListener(values)) {
                setValues(values);
            }
        }
        mPreferenceChanged = false;
    
protected java.lang.ObjectonGetDefaultValue(android.content.res.TypedArray a, int index)

        final CharSequence[] defaultValues = a.getTextArray(index);
        final int valueCount = defaultValues.length;
        final Set<String> result = new HashSet<String>();
        
        for (int i = 0; i < valueCount; i++) {
            result.add(defaultValues[i].toString());
        }
        
        return result;
    
protected voidonPrepareDialogBuilder(android.app.AlertDialog.Builder builder)

        super.onPrepareDialogBuilder(builder);
        
        if (mEntries == null || mEntryValues == null) {
            throw new IllegalStateException(
                    "MultiSelectListPreference requires an entries array and " +
                    "an entryValues array.");
        }
        
        boolean[] checkedItems = getSelectedItems();
        builder.setMultiChoiceItems(mEntries, checkedItems,
                new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            mPreferenceChanged |= mNewValues.add(mEntryValues[which].toString());
                        } else {
                            mPreferenceChanged |= mNewValues.remove(mEntryValues[which].toString());
                        }
                    }
                });
        mNewValues.clear();
        mNewValues.addAll(mValues);
    
protected android.os.ParcelableonSaveInstanceState()

        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state
            return superState;
        }
        
        final SavedState myState = new SavedState(superState);
        myState.values = getValues();
        return myState;
    
protected voidonSetInitialValue(boolean restoreValue, java.lang.Object defaultValue)

        setValues(restoreValue ? getPersistedStringSet(mValues) : (Set<String>) defaultValue);
    
public voidsetEntries(java.lang.CharSequence[] entries)
Sets the human-readable entries to be shown in the list. This will be shown in subsequent dialogs.

Each entry must have a corresponding index in {@link #setEntryValues(CharSequence[])}.

param
entries The entries.
see
#setEntryValues(CharSequence[])

        mEntries = entries;
    
public voidsetEntries(int entriesResId)

see
#setEntries(CharSequence[])
param
entriesResId The entries array as a resource.

        setEntries(getContext().getResources().getTextArray(entriesResId));
    
public voidsetEntryValues(java.lang.CharSequence[] entryValues)
The array to find the value to save for a preference when an entry from entries is selected. If a user clicks on the second item in entries, the second item in this array will be saved to the preference.

param
entryValues The array to be used as values to save for the preference.

        mEntryValues = entryValues;
    
public voidsetEntryValues(int entryValuesResId)

see
#setEntryValues(CharSequence[])
param
entryValuesResId The entry values array as a resource.

        setEntryValues(getContext().getResources().getTextArray(entryValuesResId));
    
public voidsetValues(java.util.Set values)
Sets the value of the key. This should contain entries in {@link #getEntryValues()}.

param
values The values to set for the key.

        mValues.clear();
        mValues.addAll(values);

        persistStringSet(values);