Sheriff Said Elahl Sheriff Said Elahl - 1 year ago 232
Android Question

Android Spinner With Multiple Selection

I am new to android and I was trying all the day to apply spinner with multiple selections from the below code (accepted answer on this question Android Spinner with multiple choice

the problem is I can't pass the listener from the main activity to the spinner Java file by the below line:

multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring, this);

the argument (this) isn't acceptable, I also tried MainActivity.this with no success. what is the problem here?

the Set method in the spinner code should receive (listener) but I don't know what is it and how to send it.

the spinner Java Code:

public class MultiSpinner extends Spinner implements
DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnCancelListener {

private List<String> items;
private boolean[] selected;
private String defaultText;
private MultiSpinnerListener listener;

public MultiSpinner(Context context) {

public MultiSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);

public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked)
selected[which] = true;
selected[which] = false;

public void onCancel(DialogInterface dialog) {
// refresh text on spinner
StringBuffer spinnerBuffer = new StringBuffer();
boolean someUnselected = false;
for (int i = 0; i < items.size(); i++) {
if (selected[i] == true) {
spinnerBuffer.append(", ");
} else {
someUnselected = true;
String spinnerText;
if (someUnselected) {
spinnerText = spinnerBuffer.toString();
if (spinnerText.length() > 2)
spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
} else {
spinnerText = defaultText;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
new String[] { spinnerText });

public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
items.toArray(new CharSequence[items.size()]), selected, this);
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
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 });

public interface MultiSpinnerListener {
public void onItemsSelected(boolean[] selected);

Activity Code:

public class DCOReader extends AppCompatActivity {

private Toolbar Toolbar;
private TextView StateText;
private TextView StateTextDesc;
private Button ReaderGo;
private List<String> DCOID = new ArrayList<>();
private List<String> ActivityDate = new ArrayList<>();
private List<String> CoreSite = new ArrayList<>();
private List<String> Snags = new ArrayList<>();
private List<String> SnagDesc = new ArrayList<>();
private List<String> Layout = new ArrayList<>();
private List<String> LayoutDesc = new ArrayList<>();
private List<String> Mapping = new ArrayList<>();
private List<String> MappingDesc = new ArrayList<>();
private List<String> MailSubject = new ArrayList<>();
private List<String> MDCOID = new ArrayList<>();
private List<String> MActivityDate = new ArrayList<>();
private List<String> MCoreSite = new ArrayList<>();
private List<String> MSnags = new ArrayList<>();
private List<String> MSnagDesc = new ArrayList<>();
private List<String> MLayout = new ArrayList<>();
private List<String> MLayoutDesc = new ArrayList<>();
private List<String> MMapping = new ArrayList<>();
private List<String> MMappingDesc = new ArrayList<>();
private List<String> MMailSubject = new ArrayList<>();
private List<String> ModifiedCoreSiteValues = new ArrayList<>();
private List<String> MultiSpinnerList = new ArrayList<>();
private String ReaderSelectedItem, CoreSitesString, modifiedcoresitestring;
private TableLayout Table;
String[] CoreSiteStringArray;

protected void onCreate(Bundle savedInstanceState) {

Toolbar = (Toolbar) findViewById(;
StateText = (TextView) findViewById(;
StateTextDesc = (TextView) findViewById(;
Table = (TableLayout) findViewById(;
ReaderGo = (Button) findViewById(;
MultiSpinner multiSpinner = (MultiSpinner) findViewById(;

assert Toolbar != null;

Bundle extras = getIntent().getExtras();
if (extras != null) {

DCOID = extras.getStringArrayList("DCOID");
ActivityDate = extras.getStringArrayList("ActivityDate");
CoreSite = extras.getStringArrayList("CoreSite");
Snags = extras.getStringArrayList("Snags");
SnagDesc = extras.getStringArrayList("SnagDesc");
Mapping = extras.getStringArrayList("Mapping");
MappingDesc = extras.getStringArrayList("MappingDesc");
Layout = extras.getStringArrayList("Layout");
LayoutDesc = extras.getStringArrayList("LayoutDesc");
ReaderSelectedItem = extras.getString("ReaderSelectedItem");
MailSubject = extras.getStringArrayList("MailSubject");

Set<String> CoreSiteValues = new HashSet<>();
CoreSitesString = ModifiedCoreSiteValues.toString();
CoreSitesString = CoreSitesString.replaceAll(",", "\",\"");
modifiedcoresitestring = CoreSitesString.replaceAll("]", "\"]");
modifiedcoresitestring = modifiedcoresitestring.replaceAll("\\[", "[\"");

multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring,this);



XML Spinner part:



ohhh, The small and miner mistake you've done is you forgot to implement the interface MultiSpinnerListener. Your DCOReader Activity should be like this,

public class DCOReader extends AppCompatActivity implements MultiSpinner.MultiSpinnerListener{