There are string-arrays which are datasources for spinners :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="regions">
<item>Analamanga</item>
<item>Diana</item>
</string-array>
<string-array name="districts_region0"> // the 0 corresponds to the item at position 0
<item>Central</item>
</string-array>
<string-array name="districts_region1"> // the 1 corresponds to the item at position 1
<item>Nosy-be</item>
<item>Sambava</item>
</string-array>
</resources>
In the onItemSelected of the region spinner I want to get either R.array.districts_region0 or R.array.districts_region1 according to the item selected position. I want to write something like this :
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
if (parent.getId() == R.id.region) {
String tag = "districts_region"+pos;
tag = "R.array."+tag;
ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(this,
Integer.parseInt(tag), android.R.layout.simple_spinner_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrict.setAdapter(districtAdapter);
} else if (parent.getId() == R.id.district) {
...
}
}
But this crashes ! So how to set dynamically the adapter of spinnerDistrict ?
Adapterto do this.