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

标签 android string expandablelistview arrays expandablelistadapter

我有一组字符串,我最初使用 ArrayAdapter 来填充 Spinner。我想使用 BaseExpandableListAdapter 而不是 Spinner。有什么方法可以将一组字符串数组值放入 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)}
    };

最佳答案

更改为:

    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 的回答。

关于android - 将字符串数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483872/

相关文章:

android - 当指针在多个 View 上时如何获取指针总数?

android - 如何确定 Realm 是否存在以及其中是否有数据

java - 我如何分离一个字符串,以便 "cat!"将是 "cat !"

C:如何以更简洁的方式将空终止符复制到结构成员?

Android ExpandableListView 和 onChildClick 不工作

android - 以编程方式修改或删除现有的 wifi 配置

java - 需要与 "A Min ago"或 "An Hour Ago"等字符串的时差

vb.net - Asc() VB 函数中的错误?

android - 在可扩展 ListView android中折叠除选定组之外的所有组

android - ExpandableListView适配器列表<对象>