title: Update spinner items based on the selected item of another spinner
body: <p>I am trying to update a spinner by a selected item from another spinner. The following code block describes how I did it.</p>
  
  <p>I could successfully update the <em>spinner3</em> using the if statement inside onItemSelected() method. But my problem is that it seems that a loop just keeps running which automatically selects value "5" from <em>spinner2</em> (so the Toast just keeps spitting out "You've chosen 5").</p>
  
  <p>When the activity is loaded, the Toast will display each selection from all three spinners. Does it mean that the first selection for each spinner is done when an adapter is set to a spinner?</p>
  
  <p>And does binding adapter3 to spinner3 (in the if statement) trigger a new selection of spinner2?</p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>public class WheelchairHelperMain extends Activity{
  
  Spinner spinner2;
  Spinner spinner3;
  ArrayAdapter&lt;CharSequence&gt; adapter3 ;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.wheelchair_helper_main);
  
      Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
      ArrayAdapter&lt;CharSequence&gt; adapter1 = ArrayAdapter.createFromResource(this, R.array.destinations_list, android.R.layout.simple_spinner_item);
      adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner1.setAdapter(adapter1);
      spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
  
      spinner2 = (Spinner)findViewById(R.id.spinner2);
      ArrayAdapter&lt;CharSequence&gt; adapter2 = ArrayAdapter.createFromResource(this,R.array.departure_timeH_list,android.R.layout.simple_spinner_item);
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner2.setAdapter(adapter2);
      spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
  
      spinner3 = (Spinner)findViewById(R.id.spinner3);
      spinner3.setOnItemSelectedListener(new MyOnItemSelectedListener());
  
  
  
  }
  
  public class MyOnItemSelectedListener implements OnItemSelectedListener{
  
  
      public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos,long id) {
          // TODO Auto-generated method stub
          //use the selected station and departure time to calculate the required time
              Toast toast = Toast.makeText(parent.getContext(),"You've chosen： " + parent.getItemAtPosition(pos), 2);
              toast.show();
  
              if (spinner2.getSelectedItem().equals("5")){
                  adapter3 = ArrayAdapter.createFromResource(WheelchairHelperMain.this,R.array.departure_timeH5M_list, android.R.layout.simple_spinner_item);
                  adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  spinner3.setAdapter(adapter3);
  
              Log.v("spinner2","5");
              }else if(spinner2.getSelectedItem().equals("6")){
  
                  adapter3 = ArrayAdapter.createFromResource(WheelchairHelperMain.this,R.array.departure_timeH6M_list, android.R.layout.simple_spinner_item);
                  adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  spinner3.setAdapter(adapter3);
  
                  Log.v("spinner2","6");
              }
  
      }
  
  
  
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          // TODO Auto-generated method stub
  
      }
  
  }
  }
  </code></pre>
  
  <p>The following are the xml files.
  This is the string-array for spinner2:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  &lt;string name="departure_timeH_prompt"&gt;何時？&lt;/string&gt;
  &lt;string-array name="departure_timeH_list"&gt;
      &lt;item&gt;5&lt;/item&gt;
      &lt;item&gt;6&lt;/item&gt;
      &lt;item&gt;  7   &lt;/item&gt;
      &lt;item&gt;  8   &lt;/item&gt;
      &lt;item&gt;  9   &lt;/item&gt;
      &lt;item&gt;  10  &lt;/item&gt;
      &lt;item&gt;  11  &lt;/item&gt;
      &lt;item&gt;  12  &lt;/item&gt;
      &lt;item&gt;  13  &lt;/item&gt;
      &lt;item&gt;  14  &lt;/item&gt;
      &lt;item&gt;  15  &lt;/item&gt;
      &lt;item&gt;  16  &lt;/item&gt;
      &lt;item&gt;  17  &lt;/item&gt;
      &lt;item&gt;  18  &lt;/item&gt;
      &lt;item&gt;  19  &lt;/item&gt;
      &lt;item&gt;  20  &lt;/item&gt;
      &lt;item&gt;  21  &lt;/item&gt;
      &lt;item&gt;  22  &lt;/item&gt;
      &lt;item&gt;  23  &lt;/item&gt;
      &lt;item&gt;  24  &lt;/item&gt;
      &lt;item&gt;  0   &lt;/item&gt;       
      &lt;/string-array&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>The string-array for spinner3:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
   &lt;string name="departure_timeM_prompt"&gt;何分？&lt;/string&gt;
   &lt;string-array name="departure_timeH5M_list"&gt;
      &lt;item&gt;  5   &lt;/item&gt;
      &lt;item&gt;  24  &lt;/item&gt;
      &lt;item&gt;  38  &lt;/item&gt;
      &lt;item&gt;  52  &lt;/item&gt;
  &lt;/string-array&gt;
  &lt;string-array name="departure_timeH6M_list"&gt;
      &lt;item&gt;  01  &lt;/item&gt;
      &lt;item&gt;  08  &lt;/item&gt;
      &lt;item&gt;  17  &lt;/item&gt;
      &lt;item&gt;  25  &lt;/item&gt;
      &lt;item&gt;  34  &lt;/item&gt;
      &lt;item&gt;  41  &lt;/item&gt;
      &lt;item&gt;  47  &lt;/item&gt;
      &lt;item&gt;  57  &lt;/item&gt;
   &lt;/string-array&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>I just found my problems.</p>
  
  <p>In the original code:</p>
  
  <pre><code>  spinner3.setAdapter(adapter3);
  </code></pre>
  
  <p>is called inside the <code>onItemSelectedListener</code>. When an adapter is set to an <code>AdapterView</code>, the first item on the adapter will be automatically chosen once. So, it will trigger the listener again.</p>
  
  <p>And since I didn't set a flag to distinguish whether the selection comes from spinner2 and put the <code>spinner3.setAdapter(adapter3)</code>, it will just keep running the "choosing first item on adapter3 -> check if the selected item is 5" loop.</p>
  
  <p>So, inserting the following flag in the original code works for me:</p>
  
  <pre><code>if (parent.getId()==2131165201){
    //code to check selected item and define new adapter3 for spinner3
  </code></pre>