我知道已经有大约一百万个主题,但请听我说完。
标题说明了一切,当我在微调器 1 中选择一个项目时,微调器 2 会获得一个特定的选项列表以供选择(然后将用于显示信息)。它本质上是一个小型通讯录。
*更新** 所有已修复且正常工作,特别感谢用户 FishTruck 的帮助(即让它工作!)
package com.your.package.name;
import java.util.ArrayList;
import java.util.List;
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 Contact extends Activity{
public Spinner spinner1, spinner2;
public Button btnSubmit;//not needed yet
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
findViews();
addItemsOnSpinner1();
addItemsOnSpinner2(0);
}
private void findViews(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
}
private void addItemsOnSpinner1() {
List<String> list = new ArrayList<String>();
list.add("Please Select");
list.add("Choice 1");
list.add("choice 2");
ArrayAdapter<String> name = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
name.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(name);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
if(arg2>0)
addItemsOnSpinner2(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
addItemsOnSpinner2(0);
}}
);
}
private void addItemsOnSpinner2(int selectedIndex) {
int positionTop = selectedIndex;
if(positionTop==0){
List<String> list = new ArrayList<String>();
list.add("Please Select");
ArrayAdapter<String> name0 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
name0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(name0);
}else if(positionTop==1){
List<String> list1 = new ArrayList<String>();
list1.add("Please Select");
list1.add("item 1");
list1.add("item 2");
ArrayAdapter<String> name1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list1);
name1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(name1);
}else if(positionTop==2){
List<String> list2 = new ArrayList<String>();
list2.add("Please Select");
list2.add("item 3");
list2.add("item 4");
ArrayAdapter<String> name2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list2);
name2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(name2);
}
}
}
希望这可以帮助!