Android 是否将多个字符串添加到ListArray?
我有大约70个字符串要添加到字符串数组中。。然后在文本视图中打印字符串数组 我做了一些家庭作业,我想我真的想把字符串添加到ListArray中 字符串位于string.xml文件中Android 是否将多个字符串添加到ListArray?,android,arrays,string,arraylist,Android,Arrays,String,Arraylist,我有大约70个字符串要添加到字符串数组中。。然后在文本视图中打印字符串数组 我做了一些家庭作业,我想我真的想把字符串添加到ListArray中 字符串位于string.xml文件中 TextView tvHerb = (TextView) findViewById(R.id.tvHerb); ArrayList<String> herbList = new ArrayList<String>(); herbList.add(R.string.angeli
TextView tvHerb = (TextView) findViewById(R.id.tvHerb);
ArrayList<String> herbList = new ArrayList<String>();
herbList.add(R.string.angelica, null);
herbList.add(R.string.anise_seed, null);
tvHerb.setText(herbList.toString());
TextView-tvHerb=(TextView)findviewbyd(R.id.tvHerb);
ArrayList=新的ArrayList();
herbList.add(R.string.angelica,null);
herbList.add(R.string.anise_seed,null);
setText(herbList.toString());
一旦布局显示在emulator上,我就会收到一个运行时错误
我迷路了!!!我还不熟悉android编程,因此非常感谢您的帮助。您需要使用
context.getString(id)
获取字符串资源
您使用2参数版本的.add
,还有什么原因吗
您可能需要
herbList.add(context.getString(R.string.angelica))编码>而不是在string.xml文件中包含每个id为的字符串。可以在string.xml中声明字符串数组
`<resources>
<string-array name="sample">
<item>YourValue1</item>
<item>YourValue2</item>
.....
<item>YourValue70</item>
</string-array>
</resources>`
`
你的价值观1
你的价值观2
.....
你的价值70
`
然后在你的活动中
`String[] sample = getResources().getStringArray(R.array.sample);
ArrayList<String> arrayList = new ArrayList<String>(sample.length);
arrayList = Arrays.asList(sample);`
`String[]sample=getResources().getStringArray(R.array.sample);
ArrayList ArrayList=新的ArrayList(sample.length);
arrayList=Arrays.asList(示例)`
首先,我同意agreco关于属性数量的回答
而且,如果只在arraylist中添加来自string ressource的字符串,我认为最好直接在Strings.xml文件中定义字符串数组
TextView tvHerb = (TextView) findViewById(R.id.tvHerb);
ArrayList<String> herbList = new ArrayList<String>();
herbList.add(R.string.angelica, null);
herbList.add(R.string.anise_seed, null);
tvHerb.setText(herbList.toString());
然而,我猜您的问题来自于您将toString应用于ArrayList的事实。toString是一种通用方法,我不确定它在数组中的行为。toString不一定适合您的确切需求,而且大多数情况下也不适合
我认为你最好做些事情,比如:
String stringToInsert =herbList.get(0);
for (int i=1;i<herbList.size();i++)
{
stringToInsert+="ANY_SEPARATOR_YOU_WANT" + herbList.get(i);
}
tvHerb.setText(stringToInsert);
stringToInsert=herbList.get(0);
对于(int i=1;它是分隔符,我不认为它可能是新行..因此我将它从TextView更改为ListView..我正在研究如何将文本获取到ListView ListView.setText不存在…ListView和TextView完全不同(在使用ListView之前请花时间阅读此内容:)关于分隔符,您可以使用“\n”转到新行。我希望将字符串分开,以便可以在应用程序中的其他位置调用该字符串。不必知道字符串的位置,我可以调用herb1$herb2$(请原谅语法)