title: Enable a currently disabled Spinner in Android
body: <p>I was fooling around with Android and my Java knowledge is limited at best (for instance, I'm perplexed by the fact that it allows inline classes!?).</p>
  
  <p>My question is as follows:</p>
  
  <p>I have a layout where there are three dropdown menus. I initialise all three of them inside onCreate().</p>
  
  <p>The first one takes its values from a string-array. The second one, however, depends on the choice of the first one and the third one depends on the choice of the second one in turn!</p>
  
  <p>I have a few string-arrays for the second Spinner but I was wondering what would be the correct way to implement a list of successively enabled Spinners. I'm tempted to just hack into it and make it work but I don't want to run the risk of it being malformed and unstable.</p>
  
  <p>So for example's sakes, my case is as if I had the <a href="http://developer.android.com/guide/tutorials/views/hello-spinner.html" rel="nofollow noreferrer">Google Spinner tutorial</a> with an extra Spinner for the moons of each planet (and then maybe for craters on each of the moons).</p>
  
  <p>In my resources, I have an arrays.xml such as:</p>
  
  <pre><code>&lt;resources&gt;
  
  &lt;string-array name="planets"&gt;
      &lt;item&gt;Mercury&lt;/item&gt;
      &lt;item&gt;Venus&lt;/item&gt;
      &lt;item&gt;Earth&lt;/item&gt;
      &lt;item&gt;Mars&lt;/item&gt;
      &lt;item&gt;Jupiter&lt;/item&gt;
      &lt;item&gt;Saturn&lt;/item&gt;
      &lt;item&gt;Uranus&lt;/item&gt;
      &lt;item&gt;Neptune&lt;/item&gt;
  &lt;/string-array&gt;
  &lt;string-array name="earth"&gt;
      &lt;item&gt;The Moon&lt;/item&gt;
  &lt;/string-array&gt;
  &lt;string-array name="mars"&gt;
      &lt;item&gt;Deimos&lt;/item&gt;
      &lt;item&gt;Phobos&lt;/item&gt;
  &lt;/string-array&gt;
  &lt;string-array name="jupiter"&gt;
      &lt;item&gt;Metis&lt;/item&gt;
      &lt;item&gt;Adrasthea&lt;/item&gt;
      &lt;item&gt;Amalthea&lt;/item&gt;
      &lt;item&gt;Thebe&lt;/item&gt;
      &lt;item&gt;Io&lt;/item&gt;
      &lt;item&gt;Europa&lt;/item&gt;
      &lt;item&gt;Ganymede&lt;/item&gt;
      &lt;item&gt;Callisto&lt;/item&gt;
   [...] etc. etc. 
  &lt;/resources&gt;
  </code></pre>
  
  <p>In the onCreate method I initialise all  three:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  
    Spinner spinner1 = (Spinner) findViewById(R.id.planetSpinner);
    Spinner spinner2 = (Spinner) findViewById(R.id.moonSpinner);
    Spinner spinner3 = (Spinner) findViewById(R.id.craterSpinner);
    spinner2.setEnabled(false);
    spinner3.setEnabled(false);
    ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
          this, R.array.planets, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(new MyOnPlanetSelectedListener());
  
  }
  </code></pre>
  
  <p>My listener class is</p>
  
  <pre><code>public class MyOnPlanetSelectedListener implements OnItemSelectedListener {
  
  public void onItemSelected(AdapterView&lt;?&gt; parent,
      View view, int pos, long id) {
    Toast.makeText(parent.getContext()), "The planet is " +
        parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
   }
  
  public void onNothingSelected(AdapterView parent) {
    // Do nothing.
    }
  }
  </code></pre>
  
  <p>So there it is. My question is where and how to implement the listeners for the successive Spinners so that they're enabled once the first one is selected. My first guess is:</p>
  
  <pre><code>    public class MyOnPlanetSelectedListener implements OnItemSelectedListener {
  
  public void onItemSelected(AdapterView&lt;?&gt; parent,
      View view, int pos, long id) {
    Toast.makeText(parent.getContext()), "The planet is " +
        parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
  
    spinner2.setEnabled(true);
  
   }
  
  public void onNothingSelected(AdapterView parent) {
    // Do nothing.
    }
  }
  </code></pre>
  
  <p>But obviously this is wrong since spinner2 is not encapsulated here.</p>
  
  <p>Then, the successive spinners should pick a string-array for their adapters depending on what the choice of the Spinners before it are. I tried encapsulating the spinners in the activity class, in the constructor, but the application crashes before running.</p>
  
  <p>Thank you very much in advance for your help.</p>