Skip to content

Instantly share code, notes, and snippets.

@asiviero
Created April 13, 2015 02:46
Show Gist options
  • Save asiviero/54219136dd5cd7341a15 to your computer and use it in GitHub Desktop.
Save asiviero/54219136dd5cd7341a15 to your computer and use it in GitHub Desktop.
package cz.destil.settleup.gui;
/*
* This is a small fix from MultiSpinner class as shown in:
* http://stackoverflow.com/questions/5015686/android-spinner-with-multiple-choice
*
* When every choice is selected in the original code, it also displays the default
* text. This is an updated version to make it work properly
*/
public class MultiSpinner extends Spinner implements
OnMultiChoiceClickListener, OnCancelListener {
private List<String> items;
private boolean[] selected;
private String defaultText;
private MultiSpinnerListener listener;
public MultiSpinner(Context context) {
super(context);
}
public MultiSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
selected[which] = true;
else
selected[which] = false;
}
@Override
public void onCancel(DialogInterface dialog) {
// refresh text on spinner
StringBuffer spinnerBuffer = new StringBuffer();
boolean allUnselected = true;
for (int i = 0; i < items.size(); i++) {
if (selected[i] == true) {
spinnerBuffer.append(items.get(i));
spinnerBuffer.append(", ");
allUnselected = false;
}
}
String spinnerText;
if (allUnselected) {
spinnerText = defaultText;
} else {
spinnerText = spinnerBuffer.toString();
if (spinnerText.length() > 2)
spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
R.layout.spinner_small_single_item,
new String[] { spinnerText });
setAdapter(adapter);
CurrentSpinnerText = spinnerText;
listener.onItemsSelected(selected);
}
@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(
items.toArray(new CharSequence[items.size()]), selected, this);
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(this);
builder.show();
return true;
}
public void setItems(List<String> items, String allText,
MultiSpinnerListener listener) {
this.items = items;
this.defaultText = allText;
this.listener = listener;
// all selected by default
selected = new boolean[items.size()];
for (int i = 0; i < selected.length; i++)
selected[i] = true;
// all text on the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, new String[] { allText });
setAdapter(adapter);
}
public interface MultiSpinnerListener {
public void onItemsSelected(boolean[] selected);
}
}
@homerunsb
Copy link

Could you tell me what the CurrentSpinnerText is?

@pinkeshdarji
Copy link

Can you please explain how to use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment