我想做一个城市的旋转器,旋转器将确定我选择的城市内的哪个地方(也将是一个旋转器)。
例如,如果我选择“纽约”并且调用地方的微调器将显示例如“邮政”和“百思买”,那么当我选择另一个城市时,它将显示“邮政”和“沃尔玛”。
我如何为所有城市开设课程,还是有其他方法可以做到这一点?
答案 0 :(得分:0)
试试这个
public class MainActivity extends AppCompatActivity {
private ArrayAdapter<City> adapter1;
private ArrayAdapter<Institutes> adapter2;
private Spinner spin1;
private Spinner spin2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
spin1 = (Spinner) findViewById(R.id.spin1);
spin2 = (Spinner) findViewById(R.id.spin2);
spin1.setAdapter(adapter1);
spin2.setAdapter(adapter2);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
City city = adapter1.getItem(position);
adapter2.clear();
adapter2.addAll(city.getInstitutesList());
spin2.setSelection(0);
adapter2.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
City city1 = new City("city1");
city1.getInstitutesList().add(new Institutes("Institutes1"));
city1.getInstitutesList().add(new Institutes("Institutes2"));
city1.getInstitutesList().add(new Institutes("Institutes3"));
City city2 = new City("city2");
city2.getInstitutesList().add(new Institutes("Institutes4"));
city2.getInstitutesList().add(new Institutes("Institutes5"));
city2.getInstitutesList().add(new Institutes("Institutes6"));
adapter1.addAll(city1, city2);
spin1.setSelection(0);
}
public static class City {
private List<Institutes> institutesList = new ArrayList<>();
private String name;
public City(String name) {
this.name = name;
}
public List<Institutes> getInstitutesList() {
return institutesList;
}
public void setInstitutesList(List<Institutes> institutesList) {
this.institutesList = institutesList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public static class Institutes {
private String name;
public Institutes(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
}