Affect Second Spinner Choices based on First Spinner

佐手、 提交于 2019-12-11 19:44:16

问题


So I can see that there are questions about this, but not in the scope of mine. I am buliding an app for Android that has two spinners. The first has an array of choices. However, I am not sure how to affect what choices the second one has based on the first one. I know that you can put in

AdapterView.OnItemSelectedListener 

but I'm not sure how to implement this. I've read on this but it's not quite what I'm looking for. I'm also curious as to how I tell the spinner which array to choose, is it in the .xml or in the .java file?


回答1:


Try this,

firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

                    string selectedValue = arg0.getSelectedItem().toString();
                    if(selectedValue.equalsIgnoreCase(string1)
                    {
                        ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, firstArray);

                        secondSpinner.setAdapter(firstAdapter);//
                    }

                   else if(selectedValue.equalsIgnoreCase(string2)
                   {
                      ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array2);

                      secondSpinner.setAdapter(firstAdapter);

                   }
            }

Hope it will help you.




回答2:


If it's a String array you could define it in XML then use getResource().getStringArray() or declare it in Java.

In your listener for the first spinner, you could do the following to set the choices for the second spinner.

secondSpinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, newArray);
secondSpinner.setAdapter(secondSpinnerAdapter);

Tested and working




回答3:


update the array list of second spinner in 1st spinner setOnItemSelectedListener

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub


                    string str=spinner1.getSelectedItem().toString();
                    if(str.equals("spinner item"))//spinner item is selected item of spinner1
                    {
                        ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, array1);
                 //adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                 spinner2.setAdapter(adapter1);//
                    }else if{
                   ArrayAdapter<String>adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array2);
                    //adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                 spinner2.setAdapter(adapter2);

        }
            }


来源:https://stackoverflow.com/questions/18648489/affect-second-spinner-choices-based-on-first-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!