java 如何从Android的xml字符串资源中检索二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11377004/
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-10-31 04:49:32  来源:igfitidea点击:

How to retrieve 2D array from xml string resource for Android?

javaandroidxmlmultidimensional-arrayandroid-xml

提问by Imon

Suppose I have stored a 2 dimensional array in android resource as shown below. How can I get them in a java collection like Arraylist?

假设我在 android 资源中存储了一个二维数组,如下所示。如何将它们放入像 Arraylist 这样的 Java 集合中?

<resources>
<string-array name="countries_array">   
<item>
    <name>Bahrain</name>
    <code>12345</code>
</item>
<item>
    <name>Bangladesh</name>
    <code>54545</code>
  </item>
<item>
    <name>India</name>
    <code>54455</code>
</item>

</string-array>
</resources>

For example in case of 1 dimensional array we can do it using

例如,在一维数组的情况下,我们可以使用

getResources().getStringArray(R.array.countries_array);

When the countries_array is like

当 country_array 就像

<resources>
<string-array name="countries_array">   
  <item>Bahrain</item>
  <item>Bangladesh</item>
  <item>India</item>
</string-array>
</resources>

回答by Squonk

The <string-array>element of a resources file can only be used for single dimension arrays. In other words, everything between <item>and </item>is considered to be a single string.

<string-array>资源文件的元素只能用于一维数组。换句话说,<item>和之间的所有内容都</item>被视为单个字符串。

If you want to store data in the way you describe (effectively pseudo-XML), you'll need to get the items as a single String[]using getStringArray(...)and parse the <name>and <codes>elements yourself.

如果您想以您描述的方式(实际上是伪 XML)存储数据,您需要将这些项目作为单个String[]usinggetStringArray(...)并自己解析<name><codes>元素。

Personally I'd possibly go with a de-limited format such as...

就我个人而言,我可能会使用分隔格式,例如...

<item>Bahrain,12345</item>

...then just use split(...).

...然后只需使用split(...).

Alternatively, define each <item>as a JSONObject such as...

或者,将每个定义<item>为 JSONObject,例如...

<item>{"name":"Bahrain","code":"12345"}</item>

回答by Basem

Instead of multi-valued entries, I wrote about anotherapproach where you can store your complex objects as an array, then suffix the name with an incremental integer. Loop through them and create a list of strongly-typed objects from there if needed.

写的不是多值条目,而是另一种方法,您可以将复杂对象存储为数组,然后使用增量整数作为名称后缀。如果需要,循环遍历它们并从那里创建强类型对象列表。

<resources>
    <array name="categories_0">
        <item>1</item>
        <item>Food</item>
    </array>
    <array name="categories_1">
        <item>2</item>
        <item>Health</item>
    </array>
    <array name="categories_2">
        <item>3</item>
        <item>Garden</item>
    </array>
<resources>

Then you can create a static method to retrieve them:

然后你可以创建一个静态方法来检索它们:

public class ResourceHelper {

    public static List<TypedArray> getMultiTypedArray(Context context, String key) {
        List<TypedArray> array = new ArrayList<>();

        try {
            Class<R.array> res = R.array.class;
            Field field;
            int counter = 0;

            do {
                field = res.getField(key + "_" + counter);
                array.add(context.getResources().obtainTypedArray(field.getInt(null)));
                counter++;
            } while (field != null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return array;
        }
    }
}

It can be consumed like this now:

现在可以这样食用:

for (TypedArray item : ResourceHelper.getMultiTypedArray(this, "categories")) {
    Category category = new Category();
    category.ID = item.getInt(0, 0);
    category.title = item.getString(1);
    mCategories.add(category);
}