我想要那个
当我从spinner1中选择一个项目时,spinner2中的值会相应地改变
例如
当我从spinner1中选择“USA”时,“洛杉矶”和“纽约”将显示在spinner2中。
当我从spinner1中选择“德国”时,“柏林”和“慕尼黑”将显示在spinner2中。
答案 0 :(得分:0)
您已在此方法的第一个Spinner
Spinner
中设置了第二个onItemSelected
adpater值。
CountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// get here State Spinner
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
尝试这样的事情:
private void setSpinner1() {
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
String[] countries = getResources().getStringArray(R.array.countries);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
loadContentOnSpinner2(countries[pos]);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void loadContentOnSpinner2(String country) {
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = null;
if(country.equals("USA"))
ArrayAdapter.createFromResource(this, R.array.usa_states, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
在arrays.xml(res / values文件夹)中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries">
<item>USA</item>
<item>...</item>
</string-array>
</resources>
<resources>
<string-array name="usa_states">
<item>New York</item>
<item>...</item>
</string-array>
</resources>
答案 2 :(得分:0)
当您使用OnItemSelectedListener选择第一个微调器时,请使用下面的代码。
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
if(pos == 1){
spinner2.setAdapter(adapter2);
}else{
spinner2.setAdapter(adapter3);
}
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});