我们如何从资源xml中获取arraylist(字符串和整数)

时间:2021-05-16 21:04:51

Hi want to get the arraylist from the resources.xml is there any code for this.please give me some suggestions.Thanks in advance

您想从resources.xml获取arraylist是否有任何代码。请给我一些建议。谢谢提前

5 个解决方案

#1


70  

Use Arrays.asList :

使用Arrays.asList:

String[] myResArray = getResources().getStringArray(R.array.my_array);
List<String> myResArrayList = Arrays.asList(myResArray);

This list will be immutable, so if you want a mutable list, just do :

这个列表是不可变的,所以如果你想要一个可变列表,只需:

List<String> myResMutableList = new ArrayList<String>(myResArrayList);

#2


26  

I have done this way:

我这样做了:

string.xml:

Define String Array:

定义字符串数组:

 <string-array name="my_string_array">
        <item>Test 1</item>
        <item>Test 2</item>
        <item>Test 3</item>
        <item>Test 4</item>
        <item>Test 5</item>
        <item>Test 6</item>

    </string-array>

In Activity.class / Fragment.class:

在Activity.class / Fragment.class中:

List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.my_string_array));

Done

#3


7  

Not entirely sure what you want, but this might be worth a read: http://developer.android.com/guide/topics/resources/string-resource.html

不完全确定你想要什么,但这可能值得一读:http://developer.android.com/guide/topics/resources/string-resource.html

<?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>

How to retrieve the array in your application with code:

如何使用代码检索应用程序中的数组:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

#4


6  

A modification to Klaus' answer, to get an ArrayList<String>:

修改Klaus的答案,得到一个ArrayList :

ArrayList<String> myResArrayList = new ArrayList<String>();
Resources res = getResources();
Collections.addAll(myResArrayList, res.getStringArray(R.array.myResArray));

#5


3  

Price_array in strings.xml is assigned to Arraylist

private ArrayList<String> price_unit;

price_unit= new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.Price_array))); 

#1


70  

Use Arrays.asList :

使用Arrays.asList:

String[] myResArray = getResources().getStringArray(R.array.my_array);
List<String> myResArrayList = Arrays.asList(myResArray);

This list will be immutable, so if you want a mutable list, just do :

这个列表是不可变的,所以如果你想要一个可变列表,只需:

List<String> myResMutableList = new ArrayList<String>(myResArrayList);

#2


26  

I have done this way:

我这样做了:

string.xml:

Define String Array:

定义字符串数组:

 <string-array name="my_string_array">
        <item>Test 1</item>
        <item>Test 2</item>
        <item>Test 3</item>
        <item>Test 4</item>
        <item>Test 5</item>
        <item>Test 6</item>

    </string-array>

In Activity.class / Fragment.class:

在Activity.class / Fragment.class中:

List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.my_string_array));

Done

#3


7  

Not entirely sure what you want, but this might be worth a read: http://developer.android.com/guide/topics/resources/string-resource.html

不完全确定你想要什么,但这可能值得一读:http://developer.android.com/guide/topics/resources/string-resource.html

<?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>

How to retrieve the array in your application with code:

如何使用代码检索应用程序中的数组:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

#4


6  

A modification to Klaus' answer, to get an ArrayList<String>:

修改Klaus的答案,得到一个ArrayList :

ArrayList<String> myResArrayList = new ArrayList<String>();
Resources res = getResources();
Collections.addAll(myResArrayList, res.getStringArray(R.array.myResArray));

#5


3  

Price_array in strings.xml is assigned to Arraylist

private ArrayList<String> price_unit;

price_unit= new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.Price_array)));