根据条件从另一个微调器填充微调器

时间:2012-02-27 12:40:05

标签: java android arrays spinner

我需要做的是如果选择了微调器1中的某个项,它需要在微调器01中显示某个数组 例如 如果微调器有一个选定的项目是红色微调器01需要显示level_array作为微调器01的下拉选项,否则显示cparklevel。 本质上,我试图显示每个停车场有多少级别。所以微调器1包含停车场名称,微调器01包含级别

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.cparkColour_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    Spinner spinner2 = (Spinner) findViewById(R.id.Spinner01);

    if(spinner.getSelectedItem().equals("Red")){


    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this, R.array.level_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter1);
    }

    else {

    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            this, R.array.cparkLevel_array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);
    }
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:4)

这样做:

spinner_1.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                            if(spinner_1.getSelectedItem().equals("Red")){
                            {
                                  // set adapter to spinner_2 here for "Red" selected
                            }
                            else
                            {
                                 // set adapter to spinner_2 for "Red" not selected
                            }
            }    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {                
            }
});