如何基于之前选择的微调器更改其他微调器?

时间:2018-05-13 14:56:30

标签: java android android-studio spinner android-spinner

我正在尝试根据我在第一个微调器中选择的内容而不使用任何json或数据库来更改我的第二个微调器。它必须改变下面的微调器取决于我们在第一个微调器中选择的内容。我正在尝试下面的代码但是不起作用

这是java代码

    Spinner gedungSpn, lantaiSpn, ruangSpn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kelas);
GedungSpinner();

        if (gedungSpn.getSelectedItemPosition() == 0){
            LantaiSpinnerRektorat();
        }else if(gedungSpn.getSelectedItemPosition() == 1){
            LantaiSpinnerGL();
        }
    }
 public void GedungSpinner(){

        gedungSpn = (Spinner) findViewById(R.id.gedung_spn);
        List<String> list = new ArrayList<String>();
        list.add("Rektorat");
        list.add("Griya Legita");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        gedungSpn.setAdapter(dataAdapter);
    }

    public void LantaiSpinnerRektorat(){

        lantaiSpn = (Spinner) findViewById(R.id.lantai_spn);
        List<String> list = new ArrayList<String>();
        list.add("Lantai 1");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        lantaiSpn.setAdapter(dataAdapter);
    }

    public void LantaiSpinnerGL(){

        lantaiSpn = (Spinner) findViewById(R.id.lantai_spn);
        List<String> list = new ArrayList<String>();
        list.add("Lantai 1");
        list.add("Lantai 2");
        list.add("Lantai 3");
        list.add("Lantai 4");
        list.add("Lantai 5");
        list.add("Lantai 6");
        list.add("Lantai 7");
        list.add("Lantai 8");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        lantaiSpn.setAdapter(dataAdapter);
    }

请帮帮我。也许一些技巧可以帮助。我也尝试在if语句上更改适配器,但它仍然无法正常工作

1 个答案:

答案 0 :(得分:1)

替换

        if (gedungSpn.getSelectedItemPosition() == 0){
            LantaiSpinnerRektorat();
        }else if(gedungSpn.getSelectedItemPosition() == 1){
            LantaiSpinnerGL();
        }

gedungSpn.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view,
                    int position, long id) {
                if (position == 0){
                   LantaiSpinnerRektorat();
                 }else if(position == 1){
                   LantaiSpinnerGL();
                 }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // TODO Auto-generated method stub

            }
        });