我想开发一个带有三个微调器的 android 应用程序。这三个微调器将包含有关汽车的信息,即品牌、型号和发动机。
- 因此,选择第一个微调器(品牌)后,应根据所选品牌添加第二个微调器(型号)的内容。
- 接下来,将选择第二个微调器,然后添加第三个微调器(引擎)。
我在 google 和 stackoverflow 上用关键字“Android Multi Spinners”和“Android listener for spinners”搜索了将近一周,但我仍然没有找到解决方案。
这是我对代码的想法的链接
所以,我有两个版本的源代码,但它们都没有工作。(编辑:现在工作)
对于第一个版本
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.Button;
import android.widget.Spinner;
public class AddCarActivity extends Activity {
private Spinner spnBrand, spnModel, spnEngine;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_car);
addItemsOnSpnBrand();
}
public void addItemsOnSpnBrand() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
List<String> list = new ArrayList<String>();
//Get Brand Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnBrand.setAdapter(dataAdapter);
addListenerOnSpnBrandItemSelection();
}
public void addListenerOnSpnBrandItemSelection() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
spnBrand.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
addItemOnSpnModel(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
}
public void addItemOnSpnModel(String inBrand) {
spnModel = (Spinner) findViewById(R.id.spnModel);
List<String> list = new ArrayList<String>();
//Get Model Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSeries.setAdapter(dataAdapter);
addListenerOnSpnModelItemSelection();
}
public void addListenerOnSpnModelItemSelection() {
spnModel = (Spinner) findViewById(R.id.spnModel);
spnModel.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
addItemOnSpnEngine(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
}
public void addItemOnSpnEngine(String inModel) {
spnEngine = (Spinner) findViewById(R.id.spnEngine);
List<String> list = new ArrayList<String>();
//Get Engine Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEngine.setAdapter(dataAdapter);
}
}
这是我的第二个版本
public class AddCarActivity extends Activity implements OnItemSelectedListener {
private Spinner spnBrand, spnModel, spnEngine;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_car);
addItemsOnSpnBrand();
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) {
int id = parent.getId();
switch (id)
{
case R.id.spnBrand:
addItemOnSpnModel(parent.getItemAtPosition(position).toString()); break;
case R.id.spnModel:
addItemOnSpnEngine(parent.getItemAtPosition(position).toString()); break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
return;
}
public void addItemsOnSpnBrand() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
List<String> list = new ArrayList<String>();
//Get Brand Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnBrand.setAdapter(dataAdapter);
spnBrand.setOnItemSelectedListener(this);
}
public void addItemOnSpnModel(String inBrand) {
spnModel = (Spinner) findViewById(R.id.spnModel);
List<String> list = new ArrayList<String>();
//Get Model Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnModel.setAdapter(dataAdapter);
spnModel.setOnItemSelectedListener(this);
}
public void addItemOnSpnEngine(String inModel) {
spnEngine = (Spinner) findViewById(R.id.spnEngine);
List<String> list = new ArrayList<String>();
//Get Engine Database and Add to List
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEngine.setAdapter(dataAdapter);
}
}
感谢所有试图帮助我的人。我刚刚在重新测试回答 Hip Hip Array 的代码时发现了问题。我在 addListenerOnSpnModelItemSelection() 中使用了错误的变量。我错误地使用 spnBrand 而不是 spnModel。现在,这两个版本都工作了,所以我更正了它们,希望它们能帮助其他尝试在 android 中使用 multi-spinners 的人。