Last active
September 3, 2016 05:51
-
-
Save arthtilva/d4ed1dc338c34f2fd3e59678efa2e510 to your computer and use it in GitHub Desktop.
MultiSelect Spinner or dialog to choose multiple items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* Created by arthtilva | |
*/ | |
public class MultiSelectDialog { | |
String[] _items = null; | |
boolean[] mSelection = null; | |
boolean[] mSelectionAtStart = null; | |
String _itemsAtStart = null; | |
ArrayAdapter<String> simple_adapter; | |
Activity activity; | |
Button button; | |
String prompt; | |
String positiveText = "Done"; | |
String negativeText = "Cancel"; | |
public MultiSelectDialog(Activity activity, Button button, String prompt) { | |
if (button == null) { | |
throw new NullPointerException("Initialize Button before MultiSelectDialog !!!"); | |
} | |
this.activity = activity; | |
this.button = button; | |
this.prompt = prompt; | |
simple_adapter = new ArrayAdapter<String>(activity, R.layout.category_spinner); | |
} | |
public void setButtonsText(String positiveText, String negativeText) { | |
this.positiveText = positiveText; | |
this.negativeText = negativeText; | |
} | |
public void showMultiDialog() { | |
AlertDialog.Builder builder = new AlertDialog.Builder(activity); | |
builder.setMultiChoiceItems(_items, mSelection, new DialogInterface.OnMultiChoiceClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) { | |
if (mSelection != null && which < mSelection.length) { | |
mSelection[which] = isChecked; | |
simple_adapter.clear(); | |
simple_adapter.add(buildSelectedItemString()); | |
} else { | |
throw new IllegalArgumentException("Argument 'which' is out of bounds."); | |
} | |
} | |
}); | |
builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length); | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
//TODO:MapActivity.setCount(getSelectedIndices().size()); | |
} | |
}); | |
builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
simple_adapter.clear(); | |
simple_adapter.add(_itemsAtStart); | |
System.arraycopy(mSelectionAtStart, 0, mSelection, 0, mSelectionAtStart.length); | |
} | |
}); | |
builder.show(); | |
} | |
private String buildSelectedItemString() { | |
StringBuilder sb = new StringBuilder(); | |
boolean foundOne = false; | |
for (int i = 0; i < _items.length; ++i) { | |
if (mSelection[i]) { | |
if (foundOne) { | |
sb.append(", "); | |
} | |
foundOne = true; | |
sb.append(_items[i]); | |
} | |
} | |
return sb.toString(); | |
} | |
public String getSelectedItemsAsString() { | |
StringBuilder sb = new StringBuilder(); | |
boolean foundOne = false; | |
for (int i = 0; i < _items.length; ++i) { | |
if (mSelection[i]) { | |
if (foundOne) { | |
sb.append(", "); | |
} | |
foundOne = true; | |
sb.append(_items[i]); | |
} | |
} | |
return sb.toString(); | |
} | |
public void setItems(String[] items) { | |
_items = items; | |
mSelection = new boolean[_items.length]; | |
mSelectionAtStart = new boolean[_items.length]; | |
simple_adapter.clear(); | |
simple_adapter.add(_items[0]); | |
Arrays.fill(mSelection, false); | |
mSelection[0] = true; | |
mSelectionAtStart[0] = true; | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setItems(List<String> items) { | |
_items = items.toArray(new String[items.size()]); | |
mSelection = new boolean[_items.length]; | |
mSelectionAtStart = new boolean[_items.length]; | |
simple_adapter.clear(); | |
simple_adapter.add(_items[0]); | |
Arrays.fill(mSelection, false); | |
mSelection[0] = true; | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setSelection(String[] selection) { | |
for (String cell : selection) { | |
for (int j = 0; j < _items.length; ++j) { | |
if (_items[j].equals(cell)) { | |
mSelection[j] = true; | |
mSelectionAtStart[j] = true; | |
} | |
} | |
} | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setSelection(List<String> selection) { | |
for (int i = 0; i < mSelection.length; i++) { | |
mSelection[i] = false; | |
mSelectionAtStart[i] = false; | |
} | |
for (String sel : selection) { | |
for (int j = 0; j < _items.length; ++j) { | |
if (_items[j].equals(sel)) { | |
mSelection[j] = true; | |
mSelectionAtStart[j] = true; | |
} | |
} | |
} | |
simple_adapter.clear(); | |
simple_adapter.add(buildSelectedItemString()); | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setSelection(int index) { | |
for (int i = 0; i < mSelection.length; i++) { | |
mSelection[i] = false; | |
mSelectionAtStart[i] = false; | |
} | |
if (index >= 0 && index < mSelection.length) { | |
mSelection[index] = true; | |
mSelectionAtStart[index] = true; | |
} else { | |
throw new IllegalArgumentException("Index " + index + " is out of bounds."); | |
} | |
simple_adapter.clear(); | |
simple_adapter.add(buildSelectedItemString()); | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setSelection(int[] selectedIndices) { | |
for (int i = 0; i < mSelection.length; i++) { | |
mSelection[i] = false; | |
mSelectionAtStart[i] = false; | |
} | |
for (int index : selectedIndices) { | |
if (index >= 0 && index < mSelection.length) { | |
mSelection[index] = true; | |
mSelectionAtStart[index] = true; | |
} else { | |
throw new IllegalArgumentException("Index " + index + " is out of bounds."); | |
} | |
} | |
simple_adapter.clear(); | |
simple_adapter.add(buildSelectedItemString()); | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public void setClearSelection() { | |
for (int i = 0; i < mSelection.length; i++) { | |
mSelection[i] = false; | |
mSelectionAtStart[i] = false; | |
} | |
simple_adapter.clear(); | |
simple_adapter.add(buildSelectedItemString()); | |
_itemsAtStart = getSelectedItemsAsString(); | |
if (_itemsAtStart.length() == 0) { | |
button.setText(prompt); | |
} else { | |
button.setText(_itemsAtStart); | |
} | |
} | |
public List<String> getSelectedStrings() { | |
List<String> selection = new LinkedList<String>(); | |
for (int i = 0; i < _items.length; ++i) { | |
if (mSelection[i]) { | |
selection.add(_items[i]); | |
} | |
} | |
return selection; | |
} | |
public List<Integer> getSelectedIndices() { | |
List<Integer> selection = new LinkedList<Integer>(); | |
for (int i = 0; i < _items.length; ++i) { | |
if (mSelection[i]) { | |
selection.add(i); | |
} | |
} | |
return selection; | |
} | |
} | |
/*============= XML Code=============*/ | |
<?xml version="1.0" encoding="utf-8"?> | |
<Button xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@android:id/text1" | |
style="?android:attr/spinnerItemStyle" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@android:color/transparent" | |
android:ellipsize="marquee" | |
android:singleLine="true" | |
android:textColor="@color/white" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment