基于以前的微调器自动填充微调器 - android

时间:2013-04-16 14:33:15

标签: android arrays spinner

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

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

  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

    }
    }

2 个答案:

答案 0 :(得分:2)

您需要将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)移至您的创建中,我建议对所有视图执行相同操作,这样每次选择微调项时都不会调用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);
    }
}

这是我建议您尝试的。

答案 1 :(得分: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);
        }