安卓string-array具有名称的项

3

我在资源文件中有一个字符串数组,其中包含几个项目。我知道如何通过名称调用数组。我想要的是能够通过名称调用数组项。资源文件允许我给项目命名为“item 1”。我该如何在Java类中按给定名称调用此项,就像这样:

String[]array = getResources().getStringArray(R.array.myarray);
String item1 = array["item1"];
String item2 = array["item2"];
String item3 = array["item3"];

在资源中,数组看起来像这样:
<string-array name="myarray">
    <item name="item1">item 1</item>
    <item name="item2">item 2</item>
    <item name="item3">item </item>
 </string-array>

1
可能是Android资源中的字典的重复问题。 - Veneet Reddy
3个回答

0

你可以使用哈希表,这样你就可以设置键和值,更多信息请参见this


0
没有这样的资源类型。您必须将任意文件放入res/raw/中,并在HashMap中手动导入它。
发布在这里 要使用原始InputStream打开这些资源,更多信息请参见这里getResources().openRawResource(R.raw.filename)

0
我想要的是能够通过名称调用数组项。资源文件允许我给该项命名为“item 1”。
Android Studio可能允许您使用name属性,但是资源中的项目不支持此属性。
文档中可以看到:
<item>

A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a <string-array> element. Beware that you must escape apostrophes and quotation marks. See Formatting and Styling, below, for information about to properly style and format your strings.

No attributes.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接