Change String-Array in Strings.xml to ArrayList

前端 未结 4 1629
孤城傲影
孤城傲影 2020-12-08 09:17

I\'m developing an Android app. I need to convert a string array into an ArrayList. I\'ve read up on this, and all have cases where you add the values to the array in the ja

相关标签:
4条回答
  • 2020-12-08 10:02

    This is the best practice without any warning

    Add string array to arraylist:

    Collections.addAll(Lines, getResources().getStringArray(R.array.Lines));
    

    To add one ArrayList to another:

    newList.addAll(oldList);
    
    0 讨论(0)
  • 2020-12-08 10:07

    If anybody wondering how to do the same in kotlin

    val universities = arrayListOf<String>(*resources.getStringArray(R.array.universities))
    

    universities will be ArrayList<String>

    0 讨论(0)
  • 2020-12-08 10:08

    Try this;

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

    and this for kotlin:

    val Lines = resources.getStringArray(R.array.Lines).toList()
    
    0 讨论(0)
  • 2020-12-08 10:17

    In strings.xml, add string array

    <string-array name="dashboard_tags">
        <item>Home</item>
        <item>Settings</item>
    </string-array>
    

    In your .java class access the string array like

    List<String> tags = Arrays.asList(getResources().getStringArray(R.array.dashboard_tags));
    
    0 讨论(0)
提交回复
热议问题