我有一组字符串,我最初使用 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/