2

到目前为止我有这个代码。模型微调器会根据用户为制造商选择的内容自动填充。(对于下面的代码,我只为 BMW 设置了它)。有没有办法根据制造商和型号填充第三个微调器(颜色)。

因此,如果您在第一个微调器上选择 BMW,然后为第二个微调器选择 M3 coupe,则第三个微调器将显示 M3_Color Array 中的所有选项。

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
  import android.widget.ArrayAdapter;
  import android.widget.Spinner;

  public class MainActivity extends Activity implements OnItemSelectedListener  {

  private String[] Manufacture  {    "Ford", 
                                    "BMW", 
                                    "Nissan", 
                                    "Toyota", 
                                    "Chevy", 
                              };

  private String[] BMW_Model = { "128i Coupe",
                             "M3 Coupe", 
                             "M5 Sedan",
                             "640i Gran Coupe",
                             "335i Coupe",
                              };


  private String[] M3_Color = { "Jerez Black Metallic",
                             "Melbourne Red Metallic", 
                             "Mineral White Metallic",
                             "Le Mans Blue Metallic",
                             };


        Spinner spinnerManufacture, spinnerModel, spinnerColor;


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


    spinnerManufacture = (Spinner) findViewById(R.id.manufacture_spinner);


     ArrayAdapter<String> MaufactureAdapter = new ArrayAdapter<String> (this,    android.R.layout.simple_spinner_item,Manufacture);


       MaufactureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerManufacture.setAdapter(MaufactureAdapter);


    spinnerManufacture.setOnItemSelectedListener(this);


    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
        // TODO Auto-generated method stub
        {
            if(parent.getId() == R.id.maufacture_spinner && position == 1)
            {
                spinnerModel = (Spinner) findViewById(R.id.model_spinner);
                ArrayAdapter<String> ModelAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, BMW_Model);
                ModelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerModel.setAdapter(ModelAdapter);


                 if(parent.getId() == R.id.model_spinner)
                 {
                 String[] Color;
                 if(spinnerMaufacture.getSelectedItem().toString.equals("BMW"))
                 {
                      if(spinnerModel.getSelectedItem().toString.equals("M3 Coupe))
                    {
                    Color = M3_Color
                    }
                }

                    spinnerColor = (Spinner) findViewById(R.id.color_spinner);
                    ArrayAdapter<String> ColorAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, M3_Color);
                    ColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinnerColor.setAdapter(ColorAdapter);
                }
            }




    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
    }
4

2 回答 2

3

您需要移到if(parent.getId() == R.id.model_spinner)外面if(parent.getId() == R.id.maufacture_spinner然后确保并调用spinnerModel.setOnItemSelectedListener(this)onCreate,为此您需要spinnerModel = (Spinner) findViewById(R.id.model_spinner)进入您的 on Create,我建议您对所有视图执行相同的操作,这样您就不会在findViewById每次选择微调器项目时调用。

编辑:

Spinner spinnerManufacture, spinnerModel, spinnerColor;
private ArrayAdapter<String> modelAdapter;
private ArrayAdapter<String> maufactureAdapter;
private ArrayAdapter<String> colorAdapter;

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

    spinnerManufacture = (Spinner) findViewById(R.id.manufacture_spinner);
    spinnerModel = (Spinner) findViewById(R.id.model_spinner);
    spinnerColor = (Spinner) findViewById(R.id.color_spinner);

    maufactureAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Manufacture);
    modelAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    colorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);

    maufactureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    modelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    colorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerColor.setAdapter(colorAdapter);
    spinnerManufacture.setAdapter(maufactureAdapter);
    spinnerModel.setAdapter(modelAdapter);

    spinnerManufacture.setOnItemSelectedListener(this);
    spinnerModel.setOnItemSelectedListener(this);
    spinnerColor.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getId() == R.id.maufacture_spinner) {
        String[] model;
        if (spinnerManufacture.getSelectedItem().toString().equals("BMW")){
            model = BMW_Model;
        } else {
            model = new String[] {"not sure what model list to use"};
        }
        modelAdapter.clear();
        modelAdapter.addAll(model);
    }
    if (parent.getId() == R.id.model_spinner) {
        String[] Color;
        if (spinnerModel.getSelectedItem().toString().equals("M3 Coupe")) {
            Color = M3_Color;
        } else if (spinnerModel.getSelectedItem().toString().equals("M5 Sedan")) {
            Color = M5_Color;
        } else {
            Color = new String[] { "not sure what color list to use" };
        }
        colorAdapter.clear();
        colorAdapter.addAll(Color);
    }
}

这是我建议你尝试的。

于 2013-04-16T15:00:59.160 回答
0

只需根据选择的内容构建字符串,如下所示。这会将颜色列表设置为选择 BMW 和 M3 Coupe 的 M3_Color,以及选择 BMW 和 M5 Sedan 时的 M5_Color。这需要对所有可能的组合进行。

        if(parent.getId() == R.id.model_spinner)
        {
                String[] Color = new String[];
                if(spinnerManufacture.getSelectedItem().toString().equals("BMW"))
                {

                     if(spinnerModel.getSelectedItem().toString().equals("M3 Coupe"))
                     {
                        Color = M3_Color;
                     }
                     else if(spinnerModel.getSelectedItem().toString().equals("M5 Sedan"))
                     {
                        Color = M5_Color;
                     }
                }
                spinnerColor = (Spinner) findViewById(R.id.color_spinner);
                ArrayAdapter<String> ColorAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, Color);
                ColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerColor.setAdapter(ColorAdapter);
        }
于 2013-04-16T14:55:39.057 回答