I need to set one spinner adapter dependent on another spinner selection.
For example:
I have 2 spinners. Call it spinner1 and spinner2.
The content of the spinner1 is "fruit, vegetables".
If spinner1 is set to "fruit", spinner2 set to "apple, mango, banana"
If spinner1 is set to "vegetable", spinner2 set to "carrot, cabbage, eggplant"
I already got the solution with the if-else method. But since my real code requires lots of lists on spinner1, it takes lots of if-else methods too to set spinner2.
Does anyone know how to use the "for" method to solve this?
Below is my current code
spinner1.setAdapter(adapterC);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinner1Selection = spinner1.getSelectedItem().toString();
if (spinner1Selection("fruit")){
spinner2.setAdapter(fruitAdapter);
}
if (spinner1Selection.equals("vegetable")){
spinner2.setAdapter(vegetableAdapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Aucun commentaire:
Enregistrer un commentaire