参考字符串数组中的机器人的strings.xml [英] reference string-arrays in android strings.xml

查看:149
本文介绍了参考字符串数组中的机器人的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); 

这篇关于参考字符串数组中的机器人的strings.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆