开发者

Get a string-array into a string

I have a set of strings I originally was using with an ArrayAdapter to populate a Spinner. Instead of a Spinner, I want to use a BaseExpandableListAdapter. Is there a way I can get a set of string-array values into a String object?

Here is my string-array

<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&g开发者_StackOverflowt;
    <item>Vanilla</item>
    <item>Butterscotch</item>
</string-array>

And here is a an excerpt of code I took from ApiDemos that shows how to populate the group and children of an expandable list.

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" }
    };

I want the String children to use four different string-arrays. Is there a way I could go about this? I'm not really sure how to get started.

Edit: I tried this, but I'm getting a resources not found exception.

    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)}
    };


Changed to:

    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)
    };

Thanks to Carl for the answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜