title: Android Spinner With Multiple Selection
body: <p>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 <a href="https://stackoverflow.com/questions/5015686/android-spinner-with-multiple-choice">Android Spinner with multiple choice</a></p>
  
  <p>the problem is I can't pass the listener from the main activity to the spinner Java file by the below line:</p>
  
  <pre><code>multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring, this);
  </code></pre>
  
  <p>the argument (this) isn't acceptable, I also tried MainActivity.this with no success. what is the problem here?</p>
  
  <p>the Set method in the spinner code should receive (listener) but I don't know what is it and how to send it.</p>
  
  <p>the spinner Java Code:</p>
  
  <pre><code>public class MultiSpinner extends Spinner implements
      DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnCancelListener {
  
  private List&lt;String&gt; 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 someUnselected = false;
      for (int i = 0; i &lt; items.size(); i++) {
          if (selected[i] == true) {
              spinnerBuffer.append(items.get(i));
              spinnerBuffer.append(", ");
          } else {
              someUnselected = true;
          }
      }
      String spinnerText;
      if (someUnselected) {
          spinnerText = spinnerBuffer.toString();
          if (spinnerText.length() &gt; 2)
              spinnerText = spinnerText.substring(0, spinnerText.length() - 2);
      } else {
          spinnerText = defaultText;
      }
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getContext(),
              android.R.layout.simple_spinner_item,
              new String[] { spinnerText });
      setAdapter(adapter);
      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&lt;String&gt; 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 &lt; selected.length; i++)
          selected[i] = true;
  
      // all text on the spinner
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getContext(),
              android.R.layout.simple_spinner_item, new String[] { allText });
      setAdapter(adapter);
  }
  
  public interface MultiSpinnerListener {
      public void onItemsSelected(boolean[] selected);
  }
  }
  </code></pre>
  
  <p>Activity Code:</p>
  
  <pre><code>public class DCOReader extends AppCompatActivity {
  
  private Toolbar Toolbar;
  private TextView StateText;
  private TextView StateTextDesc;
  private Button ReaderGo;
  private List&lt;String&gt; DCOID = new ArrayList&lt;&gt;();
  private List&lt;String&gt; ActivityDate = new ArrayList&lt;&gt;();
  private List&lt;String&gt; CoreSite = new ArrayList&lt;&gt;();
  private List&lt;String&gt; Snags = new ArrayList&lt;&gt;();
  private List&lt;String&gt; SnagDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; Layout = new ArrayList&lt;&gt;();
  private List&lt;String&gt; LayoutDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; Mapping = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MappingDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MailSubject = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MDCOID = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MActivityDate = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MCoreSite = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MSnags = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MSnagDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MLayout = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MLayoutDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MMapping = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MMappingDesc = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MMailSubject = new ArrayList&lt;&gt;();
  private List&lt;String&gt; ModifiedCoreSiteValues = new ArrayList&lt;&gt;();
  private List&lt;String&gt; MultiSpinnerList = new ArrayList&lt;&gt;();
  private String ReaderSelectedItem, CoreSitesString, modifiedcoresitestring;
  private TableLayout Table;
  String[] CoreSiteStringArray;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_dcoreader);
  
      Toolbar = (Toolbar) findViewById(R.id.DCOReadertoolbar);
      StateText = (TextView) findViewById(R.id.ReaderState);
      StateTextDesc = (TextView) findViewById(R.id.ReaderStateDescLabel);
      Table = (TableLayout) findViewById(R.id.DCOReaderTable);
      ReaderGo = (Button) findViewById(R.id.ReaderGo);
      MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner);
  
  
      assert Toolbar != null;
      Toolbar.setLogo(R.drawable.analyzer);
      setSupportActionBar(Toolbar);
      getSupportActionBar().setHomeButtonEnabled(true);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  
      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&lt;String&gt; CoreSiteValues = new HashSet&lt;&gt;();
      CoreSiteValues.addAll(CoreSite);
      ModifiedCoreSiteValues.addAll(CoreSiteValues);
      CoreSitesString = ModifiedCoreSiteValues.toString();
      CoreSitesString = CoreSitesString.replaceAll(",", "\",\"");
      modifiedcoresitestring = CoreSitesString.replaceAll("]", "\"]");
      modifiedcoresitestring = modifiedcoresitestring.replaceAll("\\[", "[\"");
  
  
      multiSpinner.setItems(MultiSpinnerList, modifiedcoresitestring,this);
  
  }
  
  }
  </code></pre>
  
  <p>XML Spinner part:</p>
  
  <pre><code>&lt;esmviewer.myandroid.com.esmviewer.MultiSpinner
      android:id="@+id/multi_spinner"
      android:layout_width="160dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="15dp"
      /&gt;
  </code></pre>