参考字符串数组中的机器人的strings.xml
问题描述:
我想引用一个字符串数组从另一个字符串数组中的strings.xml。如果我尝试读取字符串数组在我的活动(称为植物),每个项目的值为null。
I'd like to reference a string-array from another string-array in the strings.xml. If I try to read the string-array (named "plants") in my activity the value of every item is null.
是否有可能获得这些价值?
Is there a possibility to get these values?
下面是strings.xml中的一部分:
Here is the part of the strings.xml:
<string name="Orlaya_grandiflora_1">Orlaya grandiflora</string>
<string name="Orlaya_grandiflora_2">Large-Flowered Orlaya</string>
<string name="Orlaya_grandiflora_3">Apiaceae</string>
<string-array name="Orlaya_grandiflora">
<item>@string/Orlaya_grandiflora_1</item>
<item>@string/Orlaya_grandiflora_2</item>
<item>@string/Orlaya_grandiflora_3</item>
</string-array>
<string-array name="plants">
<item>@array/Ginkgo_biloba</item>
<item>@array/Capsicum_frutescens</item>
<item>@array/Viscum_album</item>
<item>@array/Orlaya_grandiflora</item>
</string-array>
我尝试访问值这样的,例如:
I try to access the values like this, for example:
String[] plantArray = resources.getStringArray(R.array.plants);
for (String plant : plantArray) {
System.out.println("--> " + plant);
}
工厂的值是在任何情况下空
The values of plant is in every case "null"
任何人都知道如何访问值?
Anybody knows how to access the values?
答
您应该做到以下几点:
//obtain the array that refrences others array
TypedArray plantArray=getResources().obtainTypedArray(R.array.plants);
//obtain the referenced arrays
CharSequence[] ginkoArray=plantArray.getTextArray(0);
//...
CharSequence[] orlayaArray=plantArray.getTextArray(3);