当我选择spinner1时,我有两个微调器,它显示类别示例作业,房地产等。并且spinner2包含与spinner1中特定类别相关的子类别,例如,如果用户在spinner1中选择作业,则spinner2应仅显示作业子类别。诸如热工作,软件,硬件等类别,如果在spinner1房地产中,然后在spinner2中,它应该显示租金,租赁等。我如何实现这一点?

最佳答案

简单来说,您必须为Spinner 1注册ItemSelectedListener并根据选择的值设置Spinner2的适配器

程式码片段:

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                if(position==1)
                  adapterForSpinner2=new ArrayAdapter..... //Depends on your data
                else if(position==2)
                   adapterForSpinner2=new ArrayAdapter.....//Depends on your data

                //Setting up adapter based on selection
                spinner2.setAdapter(adapterForSpinner2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });

08-17 02:02