我有两个适配器,一个适用于国家,另一个适用于各大洲。
我想要发生的事情是:当我选择欧洲大陆的旋转器中的一个大陆时,例如"亚洲",其他国家是:&#34 ;比利时","法国","意大利","德国","西班牙"点击国家/地区后,我们不会在列表中显示旋转器即可。因此,只有"中国"将显示在列表中。
注意:我不知道代码是否正常工作我只是根据github帖子中的大陆代码编辑它。
我复制代码的链接:https://stackoverflow.com/questions/ask
国家适配器和微调器:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain", "China"
};
Continents Adapter和Spinner:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, CONTINENTS);
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.continents_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Asia", "Europe", "North America", "South America"
};
答案 0 :(得分:0)
你可以试试这个
Continents Adapter和Spinner:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, CONTINENTS);
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.continents_list);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
String selectedContinent=CONTINENTS[pos];
if(selectedContinente.equals("Asia"){
//Code to generate another spinner for countries
setCountrySpinner();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private static final String[] CONTINENTS = new String[] {
"Asia", "Europe", "North America", "South America"
};
private static final String[] ASIA_COUNTRIES = new String[] {
"India","China"
};
private static final String[] OTHER_COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
private void setCountrySpinner(){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, ASIA_COUNTRIES);
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
}
使用这种方式你需要为不同的国家制作不同的数组。 这不是最好的方法。我建议你为此制作一个Model类:
private class CountryModel{
private String continentName;
private String countryName;
}
使用此模型类,您可以根据ContinentName获取国家/地区名称。
答案 1 :(得分:0)
对于国家而不是字符串数组:
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain", "China"
};
使用String数组的StringList:
private static ArrayList COUNTRIES = new ArrayList<>();
arrayList.add(new String[]{"Belgium", "France", "Italy", "Germany", "Spain"});
arrayList.add(new String[]{"China"});
现在根据用户在微调器上的选择位置,您只需显示ArrayList中该位置的String Array的国家/地区。