|
@@ -12,9 +12,7 @@ import android.view.View;
|
|
|
import com.afollestad.materialdialogs.MaterialDialog;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* This class only works on Honeycomb (API 11) and above.
|
|
@@ -41,33 +39,11 @@ public class MaterialMultiSelectListPreference extends MultiSelectListPreference
|
|
|
setWidgetLayoutResource(0);
|
|
|
}
|
|
|
|
|
|
- private String[] getPersistedValues() {
|
|
|
- Set<String> values = getPreferenceManager().getSharedPreferences().getStringSet(getKey(), null);
|
|
|
- if (values == null)
|
|
|
- return null;
|
|
|
- return values.toArray(new String[values.size()]);
|
|
|
- }
|
|
|
-
|
|
|
- private Integer[] getPersistedIndices() {
|
|
|
- String[] values = getPersistedValues();
|
|
|
- if (values == null)
|
|
|
- return null;
|
|
|
- List<Integer> indices = new ArrayList<>();
|
|
|
- for (String v : values)
|
|
|
- indices.add(findIndexOfValue(v));
|
|
|
- return indices.toArray(new Integer[indices.size()]);
|
|
|
- }
|
|
|
-
|
|
|
- private void persistValues(Integer[] indices) {
|
|
|
- final CharSequence[] values = getEntryValues();
|
|
|
- Set<String> valueSet = new HashSet<>();
|
|
|
- for (int index : indices)
|
|
|
- valueSet.add(values[index].toString());
|
|
|
- getEditor().putStringSet(getKey(), valueSet).commit();
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
protected void showDialog(Bundle state) {
|
|
|
+ List<Integer> indicies = new ArrayList<>();
|
|
|
+ for (String s : getValues())
|
|
|
+ indicies.add(findIndexOfValue(s));
|
|
|
MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
|
|
|
.title(getDialogTitle())
|
|
|
.content(getDialogMessage())
|
|
@@ -75,16 +51,14 @@ public class MaterialMultiSelectListPreference extends MultiSelectListPreference
|
|
|
.negativeText(getNegativeButtonText())
|
|
|
.positiveText(getPositiveButtonText())
|
|
|
.items(getEntries())
|
|
|
- .itemsCallbackMultiChoice(getPersistedIndices(), new MaterialDialog.ListCallbackMulti() {
|
|
|
+ .itemsCallbackMultiChoice(indicies.toArray(new Integer[indicies.size()]), new MaterialDialog.ListCallbackMulti() {
|
|
|
@Override
|
|
|
public void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
|
|
|
onClick(null, DialogInterface.BUTTON_POSITIVE);
|
|
|
dialog.dismiss();
|
|
|
- if (callChangeListener(which) && isPersistent()) {
|
|
|
- persistValues(which);
|
|
|
- }
|
|
|
}
|
|
|
- });
|
|
|
+ })
|
|
|
+ .dismissListener(this);
|
|
|
|
|
|
final View contentView = onCreateDialogView();
|
|
|
if (contentView != null) {
|