如果您具有数组名称,如何在string.xml中获取字符串数组的ID [英] How to get id of a string array in string.xml if you have the array name
本文介绍了如果您具有数组名称,如何在string.xml中获取字符串数组的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种简单的方法来获取使用其字符串名称在string.xml中定义的字符串数组的ID吗? 我有一个字符串数组的字符串名称,我需要一种引用该数组的方法.下面是一个示例xml.
I there a simple way to get the id of the string array defined in string.xml using it's string name? I have a string name of the string array, i need a way to reference that array. Below is the just an sample xml.
<string-array name="categories_array">
<item>Clothes</item>
<item>Electronics</item>
<item>Gifts</item>
<item>Food</item>
</string-array>
<string-array name="clothes">
<item>Clothes</item>
<item>Electronics</item>
<item>Gifts</item>
<item>Food</item>
<item>Books</item>
<item>Music</item>
<item>Bags</item>
</string-array>
<string-array name="electronics">
<item>Clothes</item>
<item>Electronics</item>
<item>Gifts</item>
<item>Food</item>
<item>Books</item>
<item>Music</item>
<item>Bags</item>
</string-array>
<string-array name="gifts">
<item>Clothes</item>
<item>Electronics</item>
<item>Gifts</item>
<item>Food</item>
<item>Books</item>
<item>Music</item>
<item>Bags</item>
</string-array>
<string-array name="food">
<item>Clothes</item>
<item>Electronics</item>
<item>Gifts</item>
<item>Food</item>
<item>Books</item>
<item>Music</item>
<item>Bags</item>
</string-array>
现在,如果我的数组名称为"clothes",我将如何获得它的ID?
Now if i have the array name "clothes" , how would i get it's id?
推荐答案
您可以在下面使用:
int resId = getResources().getIdentifier("nameOfStringResource", "array", getPackageName());
例如:
int resId = getResources().getIdentifier("categories_array", "array", getPackageName());