Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've created MultiSelectListPreference for devices running Android in the API earlier than level 11. </p> <ul> <li>Supports ChangeListener receiving list of selected values. </li> <li>Supports automatically setting of summary. </li> <li>Examples attached.</li> </ul> <p><a href="https://gist.github.com/cardil/4754571" rel="nofollow">https://gist.github.com/cardil/4754571</a></p> <pre><code>package pl.wavesoftware.widget; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.res.TypedArray; import android.preference.ListPreference; import android.util.AttributeSet; public class MultiSelectListPreference extends ListPreference { private String separator; private static final String DEFAULT_SEPARATOR = "\u0001\u0007\u001D\u0007\u0001"; private boolean[] entryChecked; public MultiSelectListPreference(Context context, AttributeSet attributeSet) { super(context, attributeSet); entryChecked = new boolean[getEntries().length]; separator = DEFAULT_SEPARATOR; } public MultiSelectListPreference(Context context) { this(context, null); } @Override protected void onPrepareDialogBuilder(Builder builder) { CharSequence[] entries = getEntries(); CharSequence[] entryValues = getEntryValues(); if (entries == null || entryValues == null || entries.length != entryValues.length) { throw new IllegalStateException( "MultiSelectListPreference requires an entries array and an entryValues " + "array which are both the same length"); } restoreCheckedEntries(); OnMultiChoiceClickListener listener = new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean val) { entryChecked[which] = val; } }; builder.setMultiChoiceItems(entries, entryChecked, listener); } private CharSequence[] unpack(CharSequence val) { if (val == null || "".equals(val)) { return new CharSequence[0]; } else { return ((String) val).split(separator); } } /** * Gets the entries values that are selected * * @return the selected entries values */ public CharSequence[] getCheckedValues() { return unpack(getValue()); } private void restoreCheckedEntries() { CharSequence[] entryValues = getEntryValues(); // Explode the string read in sharedpreferences CharSequence[] vals = unpack(getValue()); if (vals != null) { List&lt;CharSequence&gt; valuesList = Arrays.asList(vals); for (int i = 0; i &lt; entryValues.length; i++) { CharSequence entry = entryValues[i]; entryChecked[i] = valuesList.contains(entry); } } } @Override protected void onDialogClosed(boolean positiveResult) { List&lt;CharSequence&gt; values = new ArrayList&lt;CharSequence&gt;(); CharSequence[] entryValues = getEntryValues(); if (positiveResult &amp;&amp; entryValues != null) { for (int i = 0; i &lt; entryValues.length; i++) { if (entryChecked[i] == true) { String val = (String) entryValues[i]; values.add(val); } } String value = join(values, separator); setSummary(prepareSummary(values)); setValueAndEvent(value); } } private void setValueAndEvent(String value) { if (callChangeListener(unpack(value))) { setValue(value); } } private CharSequence prepareSummary(List&lt;CharSequence&gt; joined) { List&lt;String&gt; titles = new ArrayList&lt;String&gt;(); CharSequence[] entryTitle = getEntries(); CharSequence[] entryValues = getEntryValues(); int ix = 0; for (CharSequence value : entryValues) { if (joined.contains(value)) { titles.add((String) entryTitle[ix]); } ix += 1; } return join(titles, ", "); } @Override protected Object onGetDefaultValue(TypedArray typedArray, int index) { return typedArray.getTextArray(index); } @Override protected void onSetInitialValue(boolean restoreValue, Object rawDefaultValue) { String value = null; CharSequence[] defaultValue; if (rawDefaultValue == null) { defaultValue = new CharSequence[0]; } else { defaultValue = (CharSequence[]) rawDefaultValue; } List&lt;CharSequence&gt; joined = Arrays.asList(defaultValue); String joinedDefaultValue = join(joined, separator); if (restoreValue) { value = getPersistedString(joinedDefaultValue); } else { value = joinedDefaultValue; } setSummary(prepareSummary(Arrays.asList(unpack(value)))); setValueAndEvent(value); } /** * Joins array of object to single string by separator * * Credits to kurellajunior on this post * http://snippets.dzone.com/posts/show/91 * * @param iterable * any kind of iterable ex.: &lt;code&gt;["a", "b", "c"]&lt;/code&gt; * @param separator * separetes entries ex.: &lt;code&gt;","&lt;/code&gt; * @return joined string ex.: &lt;code&gt;"a,b,c"&lt;/code&gt; */ protected static String join(Iterable&lt;?&gt; iterable, String separator) { Iterator&lt;?&gt; oIter; if (iterable == null || (!(oIter = iterable.iterator()).hasNext())) return ""; StringBuilder oBuilder = new StringBuilder(String.valueOf(oIter.next())); while (oIter.hasNext()) oBuilder.append(separator).append(oIter.next()); return oBuilder.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload