我正在使用以下链接提供的代码 http://android-helper4u.blogspot.in/2013/03/d-databse-and-spinner-tutorial.html
如何为两个微调器编写setonItemSelectedListener。我想从数据库中获取微调器的选定数据。
答案 0 :(得分:0)
试试这段代码。 声明字符串如下所示
private String[] colors1 = {" Black"," Brown"," Red"," Orange"," Yellow"," Green"," Blue"," Violet"," Grey"," White"};
private String[] colors2={" Black"," Brown"," Red"," Orange"," Yellow"," Green"," Blue"," Violet"," Gold"," Silver"};
String selstate1,selstate2;
并在oncreate()中输入此代码
ArrayAdapter<String> adapter_state1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, colors1);
adapter_state1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter_state1);
spinner1.setSelection(0);
selstate1 = (String) spinner1.getSelectedItem();
ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, colors1);
adapter_state2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter_state2);
spinner2.setSelection(0);
selstate2 = (String) spinner2.getSelectedItem();
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
spinner3.setOnItemSelectedListener(this);
spinner4.setOnItemSelectedListener(this);
让您的活动实现OnItemSelectedListener并使用switch case
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
switch (position) {
case R.id.spinner1:
spinner1.setSelection(position);
selstate1 = (String) spinner1.getSelectedItem();
break;
case R.id.spinner2:
spinner2.setSelection(position);
selstate2 = (String) spinner2.getSelectedItem();
break;
微调器的选定项目分别存储在selstate1和selstate2字符串中