我需要从一个微调器获取selectedItemPosition并在第二个微调器中使用它。我希望这个值动态变化。
以下是我使用微调器的类代码:
int item;
Spinner spinner_cat;
Spinner spinner_subcat;
.....
spinner_cat = (Spinner) findViewById(R.id.spinner1);
List<String> category = dbAdapter.getAllCategory();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, category);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_cat.setAdapter(dataAdapter);
spinner_cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
ExpenseDB.this.item = spinner_cat
.getSelectedItemPosition() + 1;
Log.i("Is it work", "in method " + item);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinner_subcat = (Spinner) findViewById(R.id.spinner2);
List<String> sub_category = dbAdapter.getAllSubCategory(item); // here it must change dynamically
ArrayAdapter<String> subDataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sub_category);
subDataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_subcat.setAdapter(subDataAdapter);
dbAdapter.close();
}
}
答案 0 :(得分:1)
spinner_cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
ExpenseDB.this.item = spinner_cat.getSelectedItemPosition() + 1;
fillsubCategory();
}
});
...
void fillsubCategory() {
spinner_subcat = (Spinner) findViewById(R.id.spinner2);
List<String> sub_category = dbAdapter.getAllSubCategory(item);
ArrayAdapter<String> subDataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sub_category);
subDataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_subcat.setAdapter(subDataAdapter);
}