how we can get the arraylist(both string and integer) from the resources xml

前端 未结 5 1647
太阳男子
太阳男子 2020-12-29 20:22

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

相关标签:
5条回答
  • 2020-12-29 21:07

    Not entirely sure what you want, but this might be worth a read: 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);
    
    0 讨论(0)
  • 2020-12-29 21:11
    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))); 
    
    0 讨论(0)
  • 2020-12-29 21:13

    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:

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

    Done

    0 讨论(0)
  • 2020-12-29 21:13

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

    ArrayList<String> myResArrayList = new ArrayList<String>();
    Resources res = getResources();
    Collections.addAll(myResArrayList, res.getStringArray(R.array.myResArray));
    
    0 讨论(0)
  • 2020-12-29 21:16

    Use 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);
    
    0 讨论(0)
提交回复
热议问题