两个微调器,spinner2取决于spinner1

时间:2012-11-08 08:51:46

标签: android

当我选择spinner1时,我有两个微调器,它显示类别示例作业,不动产等。而spinner2包含与spinner1中的特定类别相关的子类别,例如如果用户在spinner1中选择作业,则spinner2应仅显示工作子类别,如热门工作,软件,硬件等如果在spinner1房地产,然后在微调二,它应显示租赁,租赁等。我怎样才能实现这一目标?

2 个答案:

答案 0 :(得分:2)

简单来说,你必须为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
            }

        });

答案 1 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ddServerName.setOnItemSelectedListener(this);
    ddServerPath.setOnItemSelectedListener(this);

}

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

    Toast.makeText(getApplicationContext(), "Item selected local "+adapterView.getItemAtPosition(i).toString(),
            Toast.LENGTH_SHORT).show();
    //sincronizare dropdouwn list
    if (view == ddServerName.getSelectedView()) ddServerPath.setSelection(i);
    else ddServerName.setSelection(i);
}