如何在adroid studio中获取嵌套微调器的索引?

yvgpqqbh  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(322)

我想做的是一个城市和另一个城市之间的公里计算器。
我已经了解到,根据第一个微调器中选择的状态,将显示对应于该状态的城市。现在我如何才能得到所选城市的位置,以便根据出发地城市和目的地城市来计算距离。

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<String> a1, a2, a3, a4, a5, a6, a7, a8;
    String Sp_Edo_Origen [] = {"Ohio",  "California", "Georgia"};
    String g1_cd_Origen [] = {"Columbus", "Dayton", "Cincinnati";
    String g2_cd_Origen [] = {"San Diego", "Los Angeles", "San Francisco"};
    String g3_cd_Origen [] = {"Atlanta", "Savannah"};
    String Sp_Edo_Dest [] = {"Puebla",  "Guanajuato", "Veracruz"};
    String g1_cd_Dest [] = {"Ohio",  "California", "Georgia"};
    String g2_cd_Dest [] = {"San Diego", "Los Angeles", "San Francisco"};
    String g3_cd_Dest [] = {"Atlanta", "Savannah"};

    @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<>(this, android.R.layout.simple_spinner_item, Sp_Edo_Origen);
        a2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, g1_cd_Origen);
        a3 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, g2_cd_Origen);
        a4 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, g3_cd_Origen);
        a5 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Sp_Edo_Dest);
        a6 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, g1_cd_Dest);
        a7 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, g2_cd_Dest);
        a8 = new ArrayAdapter<>(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<?> a, View v, int p, long id) {
        //Toast.makeText(this, "Pos: "+p, Toast.LENGTH_SHORT).show();
        //Toast.makeText(this, "Valor: "+Sp_Edo_Origen[p], Toast.LENGTH_SHORT).show();
        //Toast.makeText(this, "Valor: "+a.getLastVisiblePosition();, Toast.LENGTH_SHORT).show();

            if (a.getId() == R.id.Sp_Edo_Origen) {

                switch (p) {
                    case 0:
                        combo2.setAdapter(a2);
                        //Toast.makeText(this, "Valor: "+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<?> parent) {

    }
}
dz6r00yl

dz6r00yl1#

内部方法 onItemSelected ,您可以找到视图id,它是此方法中的第二个参数。将此id与城市微调器的id进行比较,如果匹配,则在填充城市适配器的城市列表中找到选中的位置(即第3个参数int p)。

相关问题