Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change String-Array in Strings.xml to ArrayList

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 java file. I have declared the string-array in the strings.xml file. My string-array is here:

<string-array name="Lines">     <item>Red Line</item>     <item>Blue Line</item>     <item>Orange Line</item>     <item>Green Line</item>     <item>Brown Line</item>     <item>Purple Line</item>     <item>Pink Line</item>     <item>Yellow Line</item> </string-array> 

I need to convert Lines into ArrayList. If I use this

List<String> Lines = new ArrayList<String>(); 

to declare the ArrayList, how can find the array list by ID, and store that as Lines?

like image 353
hichris123 Avatar asked Oct 01 '13 22:10

hichris123


People also ask

Can you convert array to ArrayList?

We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.

How do I convert a string to an array of elements?

Way 1: Using a Naive ApproachGet the string. Create a character array of the same length as of string. Traverse over the string to copy character at the i'th index of string to i'th index in the array. Return or perform the operation on the character array.


1 Answers

Try this;

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

and this for kotlin:

val Lines = resources.getStringArray(R.array.Lines).toList() 
like image 163
Melih Mucuk Avatar answered Sep 27 '22 22:09

Melih Mucuk