您想从resources.xml获取arraylist是否有任何代码。请给我一些建议。谢谢提前
使用Arrays.asList :
String[] myResArray = getResources().getStringArray(R.array.my_array); List myResArrayList = Arrays.asList(myResArray);
这个列表是不可变的,所以如果你想要一个可变列表,只需:
List myResMutableList = new ArrayList (myResArrayList);
我这样做了:
string.xml:
定义字符串数组 :
- Test 1
- Test 2
- Test 3
- Test 4
- Test 5
- Test 6
在Activity.class / Fragment.class中 :
List myArrayList = Arrays.asList(getResources().getStringArray(R.array.my_string_array));
完成
不完全确定你想要什么,但这可能值得一读: http : //developer.android.com/guide/topics/resources/string-resource.html
- Mercury
- Venus
- Earth
- Mars
如何使用代码检索应用程序中的数组:
Resources res = getResources(); String[] planets = res.getStringArray(R.array.planets_array);
修改Klaus的答案,得到一个ArrayList
:
ArrayList myResArrayList = new ArrayList (); Resources res = getResources(); Collections.addAll(myResArrayList, res.getStringArray(R.array.myResArray));
Price_array in strings.xml is assigned to Arraylist private ArrayList price_unit; price_unit= new ArrayList (Arrays.asList(getResources().getStringArray(R.array.Price_array)));