将字符串数组转换为字符串

时间:2010-12-19 16:33:59

标签: android string expandablelistview arrays expandablelistadapter

我最初使用一组字符串ArrayAdapter来填充Spinner。我想使用Spinner而不是BaseExpandableListAdapter。有没有办法让一组字符串数组值成为String对象?

这是我的字符串数组

<string-array name="component2_color">
    <item>Blueberry</item>
    <item>Chocolate</item>
    <item>Dark Chocolate</item>
    <item>Lemon</item>
    <item>Lime</item>
    <item>Orange</item>
    <item>Strawberry</item>
    <item>Raspberry</item>
    <item>Grape</item>
    <item>Vanilla</item>
    <item>Butterscotch</item>
</string-array>

以下是我从ApiDemos中获取的代码的摘录,其中显示了如何填充可扩展列表中的组和子代。

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };

我希望String个孩子使用四个不同的字符串数组。我有办法解决这个问题吗?我不太确定如何开始。

编辑:我试过这个,但是我找到了资源未找到的例外。

    private String[] groups = {UserMessage.this.getString(R.array.message_groups)};
    private String[][] children = {{UserMessage.this.getString(R.array.message_group_1)},
            {UserMessage.this.getString(R.array.message_group_2)},
            {UserMessage.this.getString(R.array.message_group_3)},
            {UserMessage.this.getString(R.array.message_group_4)}
    };

1 个答案:

答案 0 :(得分:2)

更改为:

    private String[] groups = getResources().getStringArray(R.array.message_groups);
    private String[][] children = {getResources().getStringArray(R.array.message_group_1),
            getResources().getStringArray(R.array.message_group_2),
            getResources().getStringArray(R.array.message_group_3),
            getResources().getStringArray(R.array.message_group_4)
    };

感谢Carl的回答。