title: How to get the index of a nested spinner in Adroid Studio?
body: <p>What I am trying to do is a kilometer calculator between one city and another.</p>
  <p>I have already achieved that depending on the state selected in the first spinner the cities that correspond to that state are displayed. Now how can I get the positions of the selected cities so that depending on the city of origin and the city of destination I can calculate the distance.</p>
  <pre><code>package com.example.mainspinner;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  
      TextView txtv1;
      TextView txtv2;
      Spinner combo1, combo2, combo3, combo4;
      ArrayAdapter&lt;String&gt; a1, a2, a3, a4, a5, a6, a7, a8;
      String Sp_Edo_Origen [] = {&quot;Ohio&quot;,  &quot;California&quot;, &quot;Georgia&quot;};
      String g1_cd_Origen [] = {&quot;Columbus&quot;, &quot;Dayton&quot;, &quot;Cincinnati&quot;;
      String g2_cd_Origen [] = {&quot;San Diego&quot;, &quot;Los Angeles&quot;, &quot;San Francisco&quot;};
      String g3_cd_Origen [] = {&quot;Atlanta&quot;, &quot;Savannah&quot;};
      String Sp_Edo_Dest [] = {&quot;Puebla&quot;,  &quot;Guanajuato&quot;, &quot;Veracruz&quot;};
      String g1_cd_Dest [] = {&quot;Ohio&quot;,  &quot;California&quot;, &quot;Georgia&quot;};
      String g2_cd_Dest [] = {&quot;San Diego&quot;, &quot;Los Angeles&quot;, &quot;San Francisco&quot;};
      String g3_cd_Dest [] = {&quot;Atlanta&quot;, &quot;Savannah&quot;};
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          txtv1 = (TextView)findViewById(R.id.view1);
          txtv2 = (TextView)findViewById(R.id.view2);
          combo1 = (Spinner)findViewById(R.id.Sp_Edo_Origen);
          combo2 = (Spinner)findViewById(R.id.Sp_Cd_Origen);
          combo3 = (Spinner)findViewById(R.id.Sp_Edo_Dest);
          combo4 = (Spinner)findViewById(R.id.Sp_Cd_Dest);
          combo1.setOnItemSelectedListener(this);
          combo2.setOnItemSelectedListener(this);
          combo3.setOnItemSelectedListener(this);
          combo4.setOnItemSelectedListener(this);
          a1 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, Sp_Edo_Origen);
          a2 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g1_cd_Origen);
          a3 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g2_cd_Origen);
          a4 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g3_cd_Origen);
          a5 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, Sp_Edo_Dest);
          a6 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g1_cd_Dest);
          a7 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g2_cd_Dest);
          a8 = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, g3_cd_Dest);
          combo1.setAdapter(a1);
          //combo2.setAdapter(a2);
          combo3.setAdapter(a5);
          //combo4.setAdapter(a6);
      }
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; a, View v, int p, long id) {
          //Toast.makeText(this, &quot;Pos: &quot;+p, Toast.LENGTH_SHORT).show();
          //Toast.makeText(this, &quot;Valor: &quot;+Sp_Edo_Origen[p], Toast.LENGTH_SHORT).show();
          //Toast.makeText(this, &quot;Valor: &quot;+a.getLastVisiblePosition();, Toast.LENGTH_SHORT).show();
  
  
  
              if (a.getId() == R.id.Sp_Edo_Origen) {
  
                  switch (p) {
                      case 0:
                          combo2.setAdapter(a2);
                          //Toast.makeText(this, &quot;Valor: &quot;+a.getLastVisiblePosition(), Toast.LENGTH_SHORT).show();
                          //caseta = 50 + 2;
                          //String resultado = String.valueOf(caseta);
                          // txtv1.setText(resultado);
                          break;
                      case 1:
                          combo2.setAdapter(a3);
                          break;
                      case 2:
                          combo2.setAdapter(a4);
                          break;
                  }
              }
  
              if (a.getId() == R.id.Sp_Edo_Dest){
  
                  switch (p) {
                      case 0:
                          combo4.setAdapter(a2);
                          break;
                      case 1:
                          combo4.setAdapter(a3);
                          break;
                      case 2:
                          combo4.setAdapter(a4);
                          break;
                  }
  
                  if (a.getId() == R.id.Sp_Cd_Dest) {
                      }
                  }
          }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
      }
  }
  </code></pre>