到目前为止我有这个代码。模型微调器会根据用户为制造商选择的内容自动填充。(对于下面的代码,我只为 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
}
}