title: How can I retrieve data from spinner and process it?
body: <p>This is the part of my code that went wrong(I omitted all the variable declaration to make it short): I need to create two spinners and retrieve values from them, say value1 and value2. Then I need to calculate something with value1 and value2, and the output should be displayed on the screen. I need this output to be updated whenever I selected a (new) item from the spinner. However, My code doesn't pass the value from the spinner to my java code at all. Can anyone help me out? Thanks a lot!</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sdpsalary_comp);
  
      //create a spinner called Spinner1
      Spinner1= (Spinner) findViewById(R.id.spinner1);
      ArrayAdapter&lt;CharSequence&gt; adapter1 = ArrayAdapter.createFromResource(this,
              R.array.cities, android.R.layout.simple_spinner_item);
  
      //cities is defined as string-array in XML    
      adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      Spinner1.setAdapter(adapter1);
      value1 = Spinner1.getSelectedItem().toString();
  
      //create another spinner called Spinner2
      Spinner2 = (Spinner) findViewById(R.id.spinner2);
      ArrayAdapter&lt;CharSequence&gt; adapter2 = ArrayAdapter.createFromResource(this,
              R.array.cities, android.R.layout.simple_spinner_item);
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      Spinner2.setAdapter(adapter2);
      value2 = Spinner2.getSelectedItem().toString();
  }
  
  // I need to update the output field whenever I changed the content from the spinners
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                             int pos, long id) {
      String output;
      switch(parent.getId()){
          case R.id.spinner1:
              value1 = Spinner1.getSelectedItem().toString();
              Output = ((TextView)findViewById(R.id.out));
              output=Target(base, value1, value2, cost); 
      //variables base, cost and method Target() are defined somewhere else
              Output.setText(output);
              break;
          case R.id.spinner2:
              value2 = Spinner2.getSelectedItem().toString();
              Output = ((TextView)findViewById(R.id.out));
              output=Target(base, value1, value2, cost); //base and cost are defined somewhere else
              Output.setText(output);
              break;
      }
  }
  
  public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  }
  </code></pre>
  
  <p>Here is part of my XML:</p>
  
  <pre><code>&lt;Spinner
      android:id="@+id/spinner1"
      android:layout_width="252dp"
      android:layout_height="46dp"
      android:layout_marginTop="32dp"
      android:spinnerMode="dropdown"
      app:layout_constraintBottom_toTopOf="@+id/NewCity"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.272"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/CurrentCity"
      app:layout_constraintVertical_bias="0.293" /&gt;
  
  &lt;Spinner
      android:id="@+id/spinner2"
      android:layout_width="250dp"
      android:layout_height="41dp"
      android:layout_marginBottom="16dp"
      android:layout_marginEnd="16dp"
      android:layout_marginStart="16dp"
      android:layout_marginTop="16dp"
      android:spinnerMode="dropdown"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.196"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/NewCity"
      app:layout_constraintVertical_bias="0.191" /&gt;
  &lt;TextView
      android:id="@+id/out"
      android:layout_width="wrap_content"
      android:layout_height="17dp"
      android:layout_marginStart="44dp"
      android:layout_marginTop="36dp"
      app:layout_constraintStart_toEndOf="@+id/TargetSalary"
      app:layout_constraintTop_toBottomOf="@+id/newCity" /&gt;
  </code></pre>
  
  <p>This is the logcat after I added spinner.setOnItemSelectedListener(this);</p>
  
  <pre><code>09-10 00:25:37.938 7797-7797/? E/AndroidRuntime: FATAL EXCEPTION: main
  Process: edu.gatech.seclass.sdpsalarycomp, PID: 7797
  java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.gatech.seclass.sdpsalarycomp/edu.gatech.seclass.sdpsalarycomp.SDPSalaryCompActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
      at android.app.ActivityThread.-wrap11(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
      at java.util.Arrays$ArrayList.get(Arrays.java:66)
      at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:344)
      at android.widget.AdapterView.getItemAtPosition(AdapterView.java:783)
      at edu.gatech.seclass.sdpsalarycomp.SDPSalaryCompActivity.onCreate(SDPSalaryCompActivity.java:60)
      at android.app.Activity.performCreate(Activity.java:6237)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>