我有这个strings_xml文件:
<resources>
<string name="a1">Maximum Arka Plan Sayısına Ulaştınız</string>
<string name="a2">Minimum Arka Plan Sayısına Ulaştınız</string>
<string name="a3">Başarıyla Arka Planı Ayarladınız</string>
<string name="a4">Arka plan orijinale döndürüldü</string>
<string name="a5">Arka plan orijinale döndürüldü</string>
<string name="a6">Arka plan orijinale döndürüldü</string>
.
.
.
我想将所有字符串添加到ArrayList中,所以我需要这样做:
alltoast.add(c.getString(R.string.a1));
alltoast.add(c.getString(R.string.a2));
alltoast.add(c.getString(R.string.a3));
alltoast.add(c.getString(R.string.a4));
.
.
.
所以我在考虑一个for循环,我只改变代码中的“1”,“2”部分,如下所示:
for (int i=0; i<50; i++ ){
alltoast.add(c.getString(R.string.a$i));
}
(alltoast是ArrayList的名称,c是方法的上下文)
答案 0 :(得分:0)
在String array中定义字符串。然后,您可以在循环中按索引访问元素。
示例:保存在res / values / strings.xml的XML文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array> </resources>
此应用程序代码检索字符串数组:
Resources res = getResources(); String[] planets = res.getStringArray(R.array.planets_array);
但事实上,你不需要循环:
alltoast.addAll(Arrays.asList(planets));