在第二个微调器的基础上填充第一个微调器

时间:2018-01-08 12:12:16

标签: android spinner

我希望我的页面有两个微调器,并希望在选择第一个微调器的基础上填充第二个微调器。我拿了两个阵列。第一个数组是第一个微调器,三个数组是第二个微调器。在从第一个微调器选择的值上,我希望第二个微调器选择相应的值。

我也引用了 get selected value from second spinner on the basis of selected value of first spinner

在这篇文章中,作者使用三个适配器为三个数组填充第二个微调器,但我想只使用两个。有可能吗?

Simple Android two spinner and submit example

此代码未显示任何错误,但应用程序尚未打开。

 Spinner spin,spin1;


String state[] = new String[]{"A","B","C"};
String A[]= new String[]{"Pink","Blue","Red","white"};
String B[]= new String[]{"Apple","Grapes","Banana","Orange"};
String C[]= new String[]{"River","Mountains","Tree","Flower"};


ArrayAdapter<String> adap;
ArrayAdapter<String> adap1;


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

    spin = findViewById(R.id.spin);
    spin1 = findViewById(R.id.spin1);

    adap = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,state);


    spin.setAdapter(adap);


    spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            Toast.makeText(MainActivity.this, ""+ state[i], Toast.LENGTH_SHORT).show();

           if (state[i].equalsIgnoreCase("A")) {
               adap1.clear();
               adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
               spin1.setAdapter(adap1);

           }
            if (state[i].equalsIgnoreCase("B")) {
                adap1.clear();
                adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,B);
                spin1.setAdapter(adap1);

            }

            if (state[i].equalsIgnoreCase("C")) {
                adap1.clear();
                adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
                spin1.setAdapter(adap1);

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }

    });



    }
}

2 个答案:

答案 0 :(得分:0)

从所有条件

中删除adap1.clear();

答案 1 :(得分:0)

检查空指针exp -

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this, "" + state[i], Toast.LENGTH_SHORT).show();
                if (adap1 != null) {
                    adap1.clear();
                }


                if (state[i].equalsIgnoreCase("A")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
                    spin1.setAdapter(adap1);

                }
                if (state[i].equalsIgnoreCase("B")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, B);
                    spin1.setAdapter(adap1);

                }

                if (state[i].equalsIgnoreCase("C")) {

                    adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
                    spin1.setAdapter(adap1);

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });