Java Android 资源 - 数组数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4326037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:56:22  来源:igfitidea点击:

Android Resource - Array of Arrays

javaandroidxml

提问by JediPotPie

I am trying to implement a resource data structure that includes an array of arrays, specifically strings. The issue I run into is how to get the sub-array objects and their specific values. Here is what my resource file looks like....

我正在尝试实现一个包含数组数组的资源数据结构,特别是字符串。我遇到的问题是如何获取子数组对象及其特定值。这是我的资源文件的样子....

<resources>
   <array name="array0">
     <item>
       <string-array name="array01">
         <item name="id">1</item>
         <item name="title">item one</item>
       </string-array>
     </item>
     <item>
       <string-array name="array02">
         <item name="id">2</item>
         <item name="title">item two</item>
       </string-array>
     </item>
     <item>
       <string-array name="array03">
         <item name="id">3</item>
         <item name="title">item three</item>
       </string-array>
     </item>
   </array>
</resources>

Then, in my Java code I retrieve the array and try to access the sub elements like so...

然后,在我的 Java 代码中,我检索数组并尝试像这样访问子元素......

TypedArray typedArray = getResources().obtainTypedArray(R.array.array0);

TypedValue typedValue = null;

typedArray.getValue(0, typedValue);

At this point the typedArray object should represent the string-array "array01", however, I don't see how to retrieve the "id" and "title" string elements. Any help would be appreciated, thanks in advance.

此时 typedArray 对象应该表示字符串数组“array01”,但是,我不知道如何检索“id”和“title”字符串元素。任何帮助将不胜感激,提前致谢。

采纳答案by Ted Hopp

You can almost do what you want. You have to declare each array separately and then an array of references. Something like this:

你几乎可以为所欲为。您必须分别声明每个数组,然后是一个引用数组。像这样的东西:

<string-array name="array01">
    <item name="id">1</item>
    <item name="title">item one</item>
</string-array>
<!-- etc. -->
<array name="array0">
    <item>@array/array01</item>
    <!-- etc. -->
</array>

Then in your code you do something like this:

然后在你的代码中你做这样的事情:

Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.array0);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; ++i) {
    int id = ta.getResourceId(i, 0);
    if (id > 0) {
        array[i] = res.getStringArray(id);
    } else {
        // something wrong with the XML
    }
}
ta.recycle(); // Important!

回答by Andi Krusch

An Array isn't a name/value pair. You only can access the elements by number. The syntax in the xml is wrong. It should be that way:

数组不是名称/值对。您只能按编号访问元素。xml 中的语法是错误的。应该是这样:

<string-array name="string_array_name">
    <item>text_string1</item>
    <item>text_string2</item>
 </string-array>

回答by Tuan Nguyen

according to @Ted Hopp answer, more elegant way:

根据@Ted Hopp 的回答,更优雅的方式是:

<integer-array name="id_array">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</integer-array>
<string-array name="name_array">
    <item>name 1</item>
    <item>name 2</item>
    <item>name 3</item>
</string-array>
<array name="array0">
    <item>@array/id_array</item>
    <item>@array/name_array</item>
</array>

make sure sub arrays row count is identical.
enjoy write code to access array cells!
android is still a kid while maintain the ugly "item" tag of the TypedArray "array0".
in my opinion, the most flexible should be:

确保子数组行数相同。
享受编写代码来访问数组单元的乐趣!
android 仍然是一个孩子,同时保持 TypedArray “array0”的丑陋“item”标签。
在我看来,最灵活的应该是:

<array name="array0">
    <integer-array name="id_array">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </integer-array>
    <string-array name="name_array">
        <item>name 1</item>
        <item>name 2</item>
        <item>name 3</item>
    </string-array>
</array>

but don't do that because that's not android way :)

但不要这样做,因为那不是 android 方式:)

回答by Bill

https://developer.android.com/guide/topics/resources/more-resources.html#Color

https://developer.android.com/guide/topics/resources/more-resources.html#Color

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

This application code retrieves each array and then obtains the first entry in each array:

此应用程序代码检索每个数组,然后获取每个数组中的第一个条目:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);