1

我有两个微调器,当我选择微调器 1 时,它显示类别示例作业、房地产等。微调器 2 包含与微调器 1 中的特定类别相关的子类别,例如如果用户在微调器 1 中选择作业,那么微调器 2 应该只显示作业子热门工作、软件、硬件等类别 如果在 spinner1 房地产中,那么在 spinner 2 中它应该显示出租、租赁等。我该如何实现?

4

2 回答 2

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
            }

        });
于 2012-11-08T08:54:42.687 回答
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);
}
于 2015-03-12T14:21:06.210 回答