Android: Relate 2 spinner/ variable to string-array
I want to do a 2 spinner activity that the second spinner depends on the
first one, similar than the state/city example. I populate the spinners
with the string arrays, so heres my
strings.xml
<string-array name="Jornadas">
<item >1</item>
<item >2</item>
<item >3</item>
<item >4</item>
<item >5</item>
</string-array>
<string-array name="partidosj1">
<item >c11</item>
<item >c12</item>
<item >c15</item>
<item >c13</item>
</string-array>
<string-array name="partidosj2">
<item >c1</item>
<item >c2</item>
<item >c5</item>
<item >c3</item>
</string-array>
//and so on....
Heres my oncreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner jornadas = (Spinner) findViewById(R.id.spinner1);
Spinner partidos = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,
R.array.Jornadas, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
jornadas.setAdapter(adapter1);
jornadas.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String a= "partidosj "+pos;
}
});
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,R.array.a,
android.R.layout.simple_spinner_item);
//Do whatever
}}
So the string "a" has the same name that the string array that I want to
populate the second spinner but I cannot refer It on the array adapter
because it has to be a int variable. Is there any way to convert the "a"
variable into a correct string-array resource?
Much appreciated!
No comments:
Post a Comment