我已经创建了多个arraylist,并且我想创建一个函数,该函数根据从微调器中选择的内容来显示arraylist中的项目。
我创建的一些数组列表
ArrayList<String> delhi = new ArrayList<String>();
delhi.add("Virgin Atlantic – London Heathrow");
delhi.add("British Airways – London Heathrow");
ArrayList<String> hongkong = new ArrayList<String>();
hongkong.add("Virgin Atlantic – London Heathrow");
hongkong.add("British Airways – London Heathrow");
ArrayList<String> shanghai = new ArrayList<String>();
shanghai.add("Virgin Atlantic – London Heathrow");
shanghai.add("British Airways – London Heathrow");
ArrayList<String> johannesburg = new ArrayList<String>();
johannesburg.add("Virgin Atlantic – London Heathrow");
johannesburg.add("British Airways – London Heathrow");
我创建的微调器中的某些值
spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<String> list = new ArrayList<>();
list.add("Delhi");
list.add("Hong Kong");
list.add("Shargai");
list.add("Johannesburg");
list.add("Lagos");
list.add("Antigua");
list.add("Barbados");
list.add("Grenada");
list.add("Havana");
list.add("Jamaica");
答案 0 :(得分:1)
如果要在微调器值更改时使用函数,请尝试在onCreate方法中将侦听器添加到微调器:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int myPosition, long myID) {
String country = spinner.getSelectedItem().toString();
switch (country){
case "Delhi":
//do something
break;
case "Hongkong":
//do something
break;
// etc,etc,etc
}
}
答案 1 :(得分:0)
您可能要使用开关盒:
switch(value) {
case "Delhi":
//Do something
break;
case "Hong Kong":
//Do something
break;
}