title: Android: How to make selected spinner item to TextView?
body: <p>I am trying to get the selected item from the spinner to be displayed DIRECTLY after it is selected. Imagine if this is like a quiz game. If you select one of the wrong ones, a TOAST will appear(which I cannot get working). If I select the right one, a different TOAST will appear and I will link it to a new activity. How can I get the onselect to output or display its selection to the textview and how can I implement an if statement for the right/wrong answers?</p>
  
  <pre><code>package com.example.test;
  
  import android.os.Bundle;
  import android.app.Activity;
  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 MainActivity extends Activity implements OnItemSelectedListener{
  
  String[] test_array;
  TextView display, selected;
  String result;
  Spinner spinner;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Spinner spinner = (Spinner) findViewById(R.id.spinner);
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
  }
  
  @Override
  public void onItemSelected(final AdapterView&lt;?&gt; parent, View view, final int pos,
          long id) {
      // TODO Auto-generated method stub
      result = (String) spinner.getSelectedItem();
      display = (TextView) findViewById (R.id.display);
      display.setText(result);
  }
  
  @Override
  public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>
  
  <p>I REVISED ACCORDING TO GOOGLE'S SAMPLES WITHIN THE SDK. HERE IS REVISED CODE.</p>
  
  <pre><code>package com.example.test;
  
  import android.os.Bundle;
  import android.app.Activity;
  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 MainActivity extends Activity{
  
  String[] test_array;
  TextView display, selected;
  String result, sSelect;
  Spinner spinner;
  int mPos;
  String mSelection;
  ArrayAdapter&lt;CharSequence&gt; mAdapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Spinner spinner = (Spinner) findViewById(R.id.spinner);
      this.mAdapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(this.mAdapter);
      OnItemSelectedListener listener = new myOnItemSelectedListener();
      spinner.setOnItemSelectedListener(listener);
  }
  
  public class myOnItemSelectedListener implements OnItemSelectedListener {
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos,
              long id) {
          // TODO Auto-generated method stub
          MainActivity.this.mPos = pos;
          MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
          TextView resultText = (TextView) findViewById(R.id.display);
          resultText.setText(MainActivity.this.mSelection);
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; parent) {
          // TODO Auto-generated method stub
  
      }
  
  }
  }
  </code></pre>
  
  <p>The textview now changes to the selected item, now <em>I will attempt the if/else.</em>
  The if/else statement causes crash.</p>
  
  <pre><code>04-03 01:38:17.648: E/AndroidRuntime(817): FATAL EXCEPTION: main
  04-03 01:38:17.648: E/AndroidRuntime(817): java.lang.NullPointerException
  04-03 01:38:17.648: E/AndroidRuntime(817):  at com.example.test.MainActivity$myOnItemSelectedListener.onItemSelected(MainActivity.java:43)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView.access$200(AdapterView.java:49)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Handler.handleCallback(Handler.java:615)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Handler.dispatchMessage(Handler.java:92)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.os.Looper.loop(Looper.java:137)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at android.app.ActivityThread.main(ActivityThread.java:4745)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at java.lang.reflect.Method.invoke(Method.java:511)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  04-03 01:38:17.648: E/AndroidRuntime(817):  at dalvik.system.NativeStart.main(Native Method)
  04-03 01:39:04.718: E/Trace(865): error opening trace file: No such file or directory (2)
  04-03 01:39:06.028: D/gralloc_goldfish(865): Emulator without GPU emulation detected.
  </code></pre>
  
  <p>Implemented code</p>
  
  <pre><code>public class myOnItemSelectedListener implements OnItemSelectedListener {
  
  @Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos,
          long id) {
      // TODO Auto-generated method stub
      MainActivity.this.mPos = pos;
      MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
      TextView resultText = (TextView) findViewById(R.id.display);
      resultText.setText(MainActivity.this.mSelection);
      if(result.equalsIgnoreCase("abc")){
      // Toast in here
       }else{
  }
  }
  </code></pre>