简体   繁体   English

如何从java中的字符串数组中获取项目?

[英]How to get items from string array in java?

// strings.xml

<string-array name="list">
    <item>Istinye Park</item>
    <item>Kanyon Istanbul</item>
    <item>Akmerkez</item>
    <item>Forum Istanbul</item>
    <item>Istanbul Cevahir</item>
    <item>Metrocity</item>
    <item>Istanbul Sapphire</item>
</string-array>

I need to access specific item of string-array from java.我需要从 java 访问字符串数组的特定项目。 I can access to string with R.string.str and to array with R.array.list .我可以访问字符串R.string.str并与阵列R.array.list But need something like R.array.list[1] to access specific item of array.但是需要像R.array.list[1]类的东西来访问数组的特定项目。

You can load it via the resources with getStringArray , then just manipulate it like a standard array.您可以使用getStringArray通过资源加载它,然后像标准数组一样操作它。

Resources res = getResources(); //assuming in an activity for example, otherwise you can provide a context.
String[] shoppingItems = res.getStringArray(R.array.shopping);
String istanbul = shoppingItems[1]; //Kanyon Istanbul

for the whole string array:对于整个字符串数组:

    String[] myStringArray = getResources().getStringArray(R.array.names);

for single item from the string-array resource对于字符串数组资源中的单个项目

    String myString = getResources().getStringArray(R.array.names)[numberOfItem];
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

您可以使用yourcontext.getResources().getStringArray(YOUR_ARRAY_ID)

使用getResources().getStringArray(R.array.shopping);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM