title: Android Spinner : Avoid onItemSelected calls during initialization
body: <p>I created an Android application with a <code>Spinner</code> and a <code>TextView</code>. I want to display the selected item from the Spinner's drop down list in the TextView. I implemented the Spinner in the <code>onCreate</code> method so when I'm running the program, it shows a value in the <code>TextView</code> (before selecting an item from the drop down list). </p>
  
  <p>I want to show the value in the TextView only after selecting an item from the drop down list. How do I do this?</p>
  
  <p>Here is my code: </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.Spinner;
  import android.widget.TextView;
  
  public class GPACal01Activity extends Activity implements OnItemSelectedListener {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);
  
          // Create an ArrayAdapter using the string array and a default spinner layout
          ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);
          // Specify the layout to use when the list of choices appears
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          // Apply the adapter to the spinner
          spinner.setAdapter(adapter);
          spinner.setOnItemSelectedListener(this);
      }
  
      public void onItemSelected(AdapterView&lt;?&gt; parent, View arg1, int pos,long id) {
          TextView textView = (TextView) findViewById(R.id.textView1);
          String str = (String) parent.getItemAtPosition(pos);
          textView.setText(str);
      }
  
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>