我想要1个Spinner依赖于另一个Spinner项

我想要1个Spinner依赖于另一个Spinner项

本文介绍了我想要1个Spinner依赖于另一个Spinner项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到我想在第一个微调器中选择中国作为国家时,所以我希望第二个微调器必须显示我的所有代码,这是由我的编码完成的..但是...!我的查询是,当我从第二个微调器中选择状态时,它会自动将其设置为第二个微调器的第一个值就像我在第二个微调器中有3个值一样-上海"-北京"-南京"当我选择北京时,它将自动返回为上海..!帮帮我..!!:)

See I want when I choose China as a country in 1st spinner, So I want that 2nd spinner have to show all states of China it is done by my this coding.. BUT...!My query is this when I choose state from second spinner it automatically set it as 1st value of second spinnerlike if i have 3 values in second spinner-"Shanghai"-"Beijing"-"Nanjing"when I chooses Beijing it automatically return as Shanghai..!!Help me out..!! :)

public class MainActivity extends Activity  implements AdapterView.OnItemSelectedListener
{

    String[] country = { "India", "Pakistan", "China"};
    String[] states_india = { "Delhi", "UP", "Bihar"};
    String[] states_china = { "Shanghai", "Beijing", "Nanjing"};
    String[] states_pak = { "Lahor", "Islamabaad", "Punjab"};
    int fee,tot=0,atot=0;
    Spinner spin0,spin1;
    private RadioGroup radioSexGroup;
    RadioButton r1,button1,button2;
    private EditText edittext1,edittext2;
    String Name;
    private TextView calc,detail;
    private Button clean;

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

        spin0 = (Spinner) findViewById(R.id.spinner1);
        spin0.setOnItemSelectedListener((OnItemSelectedListener) this);
        ArrayAdapter <String> c = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,country);
        c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin0.setAdapter(c);

        spin1 = (Spinner) findViewById(R.id.spinner2);
        spin1.setOnItemSelectedListener((OnItemSelectedListener) this);
        spin1.setEnabled(false);

    //<!-- © 2013 Parth Shrarma -->
        addListenerOnButton();
    }

    private void addListenerOnButton() {
        radioSexGroup = (RadioGroup)  findViewById(R.id.radioGroup1);
        button1 = (RadioButton)  findViewById(R.id.radioButton1);
        button2 = (RadioButton)  findViewById(R.id.radioButton2);
        edittext1=(EditText) findViewById(R.id.editText1);
        edittext2=(EditText) findViewById(R.id.editText2);
        detail=(TextView) findViewById(R.id.textView5);
        calc=(TextView) findViewById(R.id.textView6);



        calc.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                StringBuilder result=new StringBuilder();


                if(!edittext1.getText().equals("") && edittext1.getText().length() > 0 )
                {
                    Name = edittext1.getText().toString();

                }
                fee = Integer.parseInt(edittext2.getText().toString());

                int selectedId = radioSexGroup.getCheckedRadioButtonId();
                r1 = (RadioButton)  findViewById(selectedId);

                result.append("Details : \nName"+ Name);


            }});

        clean=(Button) findViewById(R.id.clear);
        clean.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                edittext1.setText("");
                detail.setText("Total : ");

            }
            //<!-- © 2013 Parth Shrarma -->
        });

    }

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

            spin1.setEnabled(true);
                        if(spin0.getSelectedItem().equals("India"))
                        {
                            ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
                            s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin1.setAdapter(s1);
                        }
                        else  if(spin0.getSelectedItem().equals("Pakistan"))
                        {
                            ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
                            s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin1.setAdapter(s2);
                        }
                        else  if(spin0.getSelectedItem().equals("China"))
                        {
                            ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
                            s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin1.setAdapter(s3);
                        }

        }
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    }

推荐答案

问题可能出在您的 ItemSelectedListener 上.为两个微调器设置相同的侦听器时,必须检查选择了哪个微调器.您可以使用 AdapterView<?>进行检查.arg0 参数.在这里,您要反复调用 setAdapter().

The problem may be with your ItemSelectedListener. As you set same listener for both spinners, must check which spinner is selected. You can check it with AdapterView<?> arg0 parameter.Here, you are calling setAdapter() repeatedly.

因此,您的代码将发生变化,

So, your code will change like,

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

if(arg0.equals(spin0)){

        spin1.setEnabled(true);
                    if(spin0.getSelectedItem().equals("India"))
                    {
                        ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
                        s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s1);
                    }
                    else  if(spin0.getSelectedItem().equals("Pakistan"))
                    {
                        ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
                        s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s2);
                    }
                    else  if(spin0.getSelectedItem().equals("China"))
                    {
                        ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
                        s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s3);
                    }
}
    }

这篇关于我想要1个Spinner依赖于另一个Spinner项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:46