Android xml中的嵌套数组 [英] Nested Arrays in Android xml

查看:146
本文介绍了Android xml中的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我有一个简单的字符串数组,其中包含一个看起来像的网址

Previously I had a simple string-array that contained a URL it looked like

    <string-array name="drawerlinkitems">
      <item>http://www.google.com</item>
      <item>http://www.google2.com</item>
      <item>http://www.google3.com</item>
    </string-array>

我可以通过电话访问价值

and i was able to access the values with the call

return getResources().getStringArray(R.array.drawerlinkitems)[number];

非常简单的东西。

我的问题在这一点上我想做更多的动作,而不仅仅是抓住网址,所以我想构建一个嵌套数组,如下所示:

my problem at this point is I want to do some more actions other than just grabbing the url, so i would like to build a nested array, like this:

<string-array name="draweritems">
    <item>
        <link>http://www.google.com</link>
        <title>Google</title>
        <icon>soon</icon>
    </item>
    <item>
        <link>http://www.google2.com</link>
        <title>Google2</title>
        <icon>soon</icon>
    </item>
    <item>
        <link>http://www.google3.com</link>
        <title>Google3</title>
        <icon>soon</icon>
    </item>
</string-array>

然后访问它,使用类似

getResources().getStringArray(R.array.draweritems)[number].getString[link];

getResources().getStringArray(R.array.draweritems)[number].getString[1];

(显然我把getString部分搞定了)

(obviously i made the getString part up)

我无法弄清楚是否有可能在字符串数组中执行此操作,如果不是,那么替换选项是什么。如果是,我不确定如何引用字符串数组以从子项中获取子值。如果有一种你知道的优越方法,我也不会受到这种解决方案的束缚。任何帮助将不胜感激。

I can't figure out if it's even possible to do this in string-arrays, and if not what the replacement option is. If it is, i'm not sure exactly how to reference the string array to get the child values out of the item parent. i'm also not tied down to this type of solution if there's a superior way to do this that you know of. any help would be much appreciated.

推荐答案

这就是我为实现这样的目标所做的:

This is what I've done to accomplish something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="menu_items">
        <item>@array/menu_item_dashboard</item>
        <item>@array/menu_item_index</item>
    </array>

    <array name="menu_item_dashboard">
        <item>@drawable/transparent</item>
        <item>Dashboard</item>
        <item>home</item>
    </array>
    <array name="menu_item_index">
        <item>@drawable/transparent</item>
        <item>Title</item>
        <item>index</item>
    </array>
</resources>

要访问:

TypedArray menuResources = getResources().obtainTypedArray(R.array.menu_items);

TypedArray itemDef;

for (int i = 0; i < menuResources.length(); i++) {
    int resId = menuResources.getResourceId(i, -1);
    if (resId < 0) {
        continue;
    }

    itemDef = getResources().obtainTypedArray(resId);
    //itemDef.getDrawable(0)
    //itemDef.getString(1)
    //itemDef.getString(2)
}

这篇关于Android xml中的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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