FileDocCategorySizeDatePackage
MultiCheckPreference.javaAPI DocAndroid 5.1 API10748Thu Mar 12 22:22:10 GMT 2015android.preference

MultiCheckPreference

public class MultiCheckPreference extends DialogPreference
hide
A {@link Preference} that displays a list of entries as a dialog which allow the user to toggle each individually on and off.
attr
ref android.R.styleable#ListPreference_entries
attr
ref android.R.styleable#ListPreference_entryValues

Fields Summary
private CharSequence[]
mEntries
private String[]
mEntryValues
private boolean[]
mSetValues
private boolean[]
mOrigValues
private String
mSummary
Constructors Summary
public MultiCheckPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)

        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.ListPreference, defStyleAttr, defStyleRes);
        mEntries = a.getTextArray(com.android.internal.R.styleable.ListPreference_entries);
        if (mEntries != null) {
            setEntries(mEntries);
        }
        setEntryValuesCS(a.getTextArray(
                com.android.internal.R.styleable.ListPreference_entryValues));
        a.recycle();

        /* Retrieve the Preference summary attribute since it's private
         * in the Preference class.
         */
        a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.Preference, 0, 0);
        mSummary = a.getString(com.android.internal.R.styleable.Preference_summary);
        a.recycle();
    
public MultiCheckPreference(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

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

        this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
    
public MultiCheckPreference(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.String[]getEntryValues()
Returns the array of values to be saved for the preference.

return
The array of values.

        return mEntryValues;
    
public java.lang.CharSequencegetSummary()
Returns the summary of this ListPreference. If the summary has a {@linkplain java.lang.String#format String formatting} marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place.

return
the summary with appropriate string substitution

        if (mSummary == null) {
            return super.getSummary();
        } else {
            return mSummary;
        }
    
public booleangetValue(int index)
Get the boolean state of a given value.

        return mSetValues[index];
    
public boolean[]getValues()
Returns the currently selected values.

        return mSetValues;
    
protected voidonDialogClosed(boolean positiveResult)

        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            if (callChangeListener(getValues())) {
                return;
            }
        }
        System.arraycopy(mOrigValues, 0, mSetValues, 0, mSetValues.length);
    
protected java.lang.ObjectonGetDefaultValue(android.content.res.TypedArray a, int index)

        return a.getString(index);
    
protected voidonPrepareDialogBuilder(android.app.AlertDialog.Builder builder)

        super.onPrepareDialogBuilder(builder);
        
        if (mEntries == null || mEntryValues == null) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array.");
        }

        mOrigValues = Arrays.copyOf(mSetValues, mSetValues.length);
        builder.setMultiChoiceItems(mEntries, mSetValues,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        mSetValues[which] = isChecked;
                    }
        });
    
protected voidonRestoreInstanceState(android.os.Parcelable state)

        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }
         
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        setValues(myState.values);
    
protected android.os.ParcelableonSaveInstanceState()

        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state since it's persistent
            return superState;
        }
        
        final SavedState myState = new SavedState(superState);
        myState.values = getValues();
        return myState;
    
protected voidonSetInitialValue(boolean restoreValue, java.lang.Object 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;
        mSetValues = new boolean[entries.length];
        mOrigValues = new boolean[entries.length];
    
public voidsetEntries(int entriesResId)

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

        setEntries(getContext().getResources().getTextArray(entriesResId));
    
public voidsetEntryValues(java.lang.String[] 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;
        Arrays.fill(mSetValues, false);
        Arrays.fill(mOrigValues, false);
    
public voidsetEntryValues(int entryValuesResId)

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

        setEntryValuesCS(getContext().getResources().getTextArray(entryValuesResId));
    
private voidsetEntryValuesCS(java.lang.CharSequence[] values)

        setValues(null);
        if (values != null) {
            mEntryValues = new String[values.length];
            for (int i=0; i<values.length; i++) {
                mEntryValues[i] = values[i].toString();
            }
        }
    
public voidsetSummary(java.lang.CharSequence summary)
Sets the summary for this Preference with a CharSequence. If the summary has a {@linkplain java.lang.String#format String formatting} marker in it (i.e. "%s" or "%1$s"), then the current entry value will be substituted in its place when it's retrieved.

param
summary The summary for the preference.

        super.setSummary(summary);
        if (summary == null && mSummary != null) {
            mSummary = null;
        } else if (summary != null && !summary.equals(mSummary)) {
            mSummary = summary.toString();
        }
    
public voidsetValue(int index, boolean state)
Set the boolean state of a given value.

        mSetValues[index] = state;
    
public voidsetValues(boolean[] values)
Sets the current values.

        if (mSetValues != null) {
            Arrays.fill(mSetValues, false);
            Arrays.fill(mOrigValues, false);
            if (values != null) {
                System.arraycopy(values, 0, mSetValues, 0,
                        values.length < mSetValues.length ? values.length : mSetValues.length);
            }
        }