title: Android Multi Spinner
body: <p>I would like to develop an android app with three spinners.
  These three spinners will contain the information about the cars, which are brands, models, and engines.</p>
  <ol>
  <li>So, after the first spinner(brand) is chosen, the contents of the second spinner(model) should be added depend on the selected brand.</li>
  <li>Next, the second spinner will be selected and then the third spinner(engine) will be added.</li>
  </ol>
  <p>I have searched with keyword &quot;Android Multi Spinners&quot; and &quot;Android listener for spinners&quot; on google and stackoverflow for almost a week, but I still didn't find the solution.</p>
  <blockquote>
  <p>Here are the links that I got the idea for my code</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/8150834/multiple-spinners-simple-doubts">multiple spinner&#39;s simple doubts</a></li>
  <li><a href="https://stackoverflow.com/questions/11227951/spinner-listener-not-working">Spinner listener not working</a></li>
  </ul>
  </blockquote>
  <p>So, I have two versions of my source code, but both of them didn't work. (Edited: Working now)</p>
  <p><strong>For the first version</strong></p>
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.Spinner;
  
  public class AddCarActivity extends Activity {
  
      private Spinner spnBrand, spnModel, spnEngine;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add_car);
  
          addItemsOnSpnBrand();   
      }
  
      public void addItemsOnSpnBrand() {
          spnBrand = (Spinner) findViewById(R.id.spnBrand);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          //Get Brand Database and Add to List
  
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnBrand.setAdapter(dataAdapter);       
  
          addListenerOnSpnBrandItemSelection();
      }
  
      public void addListenerOnSpnBrandItemSelection() {
          spnBrand = (Spinner) findViewById(R.id.spnBrand);       
          spnBrand.setOnItemSelectedListener(new OnItemSelectedListener() {
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) {
                  addItemOnSpnModel(parent.getItemAtPosition(pos).toString());
              }
  
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
                  return;
              }
          });
      }
  
      public void addItemOnSpnModel(String inBrand) {
          spnModel = (Spinner) findViewById(R.id.spnModel);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          //Get Model Database and Add to List
  
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnSeries.setAdapter(dataAdapter);      
  
          addListenerOnSpnModelItemSelection();
      }
  
      public void addListenerOnSpnModelItemSelection() {
          spnModel = (Spinner) findViewById(R.id.spnModel);
          spnModel.setOnItemSelectedListener(new OnItemSelectedListener() {
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) {
                  addItemOnSpnEngine(parent.getItemAtPosition(pos).toString());               
              }
  
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
                  return;
              }
          });
      }
  
      public void addItemOnSpnEngine(String inModel) {
          spnEngine = (Spinner) findViewById(R.id.spnEngine);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      
          //Get Engine Database and Add to List
  
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnEngine.setAdapter(dataAdapter);
      }
  }
  </code></pre>
  <p><strong>Here is my Second version</strong></p>
  <pre><code>public class AddCarActivity extends Activity implements OnItemSelectedListener {
  
      private Spinner spnBrand, spnModel, spnEngine;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add_car);
  
          addItemsOnSpnBrand();
      }
  
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position,long arg3) {
          int id = parent.getId();
          switch (id) 
          {
              case R.id.spnBrand:
                  addItemOnSpnModel(parent.getItemAtPosition(position).toString()); break;
              case R.id.spnModel:
                  addItemOnSpnEngine(parent.getItemAtPosition(position).toString()); break;
          }
      }
  
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          return;
      }
  
      public void addItemsOnSpnBrand() {
          spnBrand = (Spinner) findViewById(R.id.spnBrand);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          //Get Brand Database and Add to List
      
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnBrand.setAdapter(dataAdapter);
  
          spnBrand.setOnItemSelectedListener(this);
      }
  
      public void addItemOnSpnModel(String inBrand) {
          spnModel = (Spinner) findViewById(R.id.spnModel);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          //Get Model Database and Add to List
  
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnModel.setAdapter(dataAdapter);
  
          spnModel.setOnItemSelectedListener(this);
      }
  
      public void addItemOnSpnEngine(String inModel) {
          spnEngine = (Spinner) findViewById(R.id.spnEngine);
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      
          //Get Engine Database and Add to List
  
          ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spnEngine.setAdapter(dataAdapter);
      }
  }
  </code></pre>
  <p>Thank you everyone that have tried to help me. I just found the problem when retest the codes for answering Hip Hip Array. I use the wrong variable in addListenerOnSpnModelItemSelection(). I mistaken to use spnBrand instead of spnModel. Now, these two versions are worked, so I corrected them and hope that they will help the others that try to use the multi-spinners in android.</p>