Android:将2个微调器/变量关联到字符串数组

时间:2013-09-28 20:09:24

标签: android arrays string spinner android-spinner

我想做一个2个微调器活动,第二个微调器依赖于第一个微调器,类似于州/城市示例。我用字符串数组填充了微调器,所以继承我的

  

的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....

继承我的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
}}

因此字符串“a”与我想要填充第二个微调器的字符串数组具有相同的名称,但我不能在数组适配器上引用它,因为它必须是一个int变量。有没有办法将“a”变量转换为正确的字符串数组资源?

非常感谢!

1 个答案:

答案 0 :(得分:0)

你去了:

int arrayId = getResources().getIdentifier(a, "array", getPackageName());