I want my page to have two spinners and want to populate the second spinner on the basis of selection of the first spinner . I took two arrays . The first array is or the 1st spinner and the three arrays are for the second spinner . On the value selected from the first spinner I want the second spinner to select the corresponding values .
I have also referenced get selected value from second spinner on the basis of selected value of first spinner
in this post , the author has used three adapters for three arrays to populate the second spinner but I was thinking of using only two . Is it possible ?
Simple Android two spinner and submit example
This code isn't showing any errors but the app isn't opening .
Spinner spin,spin1;
String state[] = new String[]{"A","B","C"};
String A[]= new String[]{"Pink","Blue","Red","white"};
String B[]= new String[]{"Apple","Grapes","Banana","Orange"};
String C[]= new String[]{"River","Mountains","Tree","Flower"};
ArrayAdapter<String> adap;
ArrayAdapter<String> adap1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = findViewById(R.id.spin);
spin1 = findViewById(R.id.spin1);
adap = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,state);
spin.setAdapter(adap);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, ""+ state[i], Toast.LENGTH_SHORT).show();
if (state[i].equalsIgnoreCase("A")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, A);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("B")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,B);
spin1.setAdapter(adap1);
}
if (state[i].equalsIgnoreCase("C")) {
adap1.clear();
adap1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, C);
spin1.setAdapter(adap1);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}