I'm building a simple app for android which consists of spinners and edittext.
question is I have 2 spinners with arrays in an adapter
Spinner 1
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.kendaraan_arrays,android.R.layout.simple_spinner_item);
spKendaraan.setAdapter(adapter1);
what I want is arrays from spinner 2 is depends on what i select in spinner 1
Spinner 2
@Override
public void onItemSelected (AdapterView < ? > adapterView, View view,int position, long id){
if (spKendaraan.getSelectedItem().equals("Mobil")) {
Toast.makeText(getApplicationContext(), "Mobil dipilih", Toast.LENGTH_SHORT).show();
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_mobil, android.R.layout.simple_spinner_item);
spPelanggaran.setAdapter(adapter2);
} else {
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_motor, android.R.layout.simple_spinner_item);
spPelanggaran.setAdapter(adapter2);
}
}
my code above doesn't work, am I making logic/method mistakes?
tl;dr
if spinnerA == x{
spinnerB = m
}
else {
spinnerB = n
}
since i'm new in android, any example would be very appreciated.
I just can see that below this line instead of
this
you could writegetApplicationContext()
orgetBaseContext()
So the actual code should be insideonItemSelected
I think it should work right now. Please let me know if you get any problem with this :)
I don't know why your code is not working but following is a example code for you which works as you needed,I hope this will help.
XML layout file:
Activity:
Strings: