Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ArrayList<String> from resource string-array

Tags:

android

kotlin

I have declared ArrayList like,

var otherSeriesList = ArrayList<String>()

And trying to get data from resource by following,

otherSeriesList = ArrayList<String>(Arrays.asList(resources.getStringArray(R.array.get_other_series)))

But I am getting error. Please see the image- enter image description here

How should I create ArrayList from resource string-array?

like image 776
Exigente05 Avatar asked Jan 11 '18 12:01

Exigente05


2 Answers

Simple do like this-

otherSeriesList = ArrayList<String>(Arrays.asList(*resources.getStringArray(R.array.get_other_series)))

* will pass the Array as vararg

like image 126
nuevo Avatar answered Oct 25 '22 22:10

nuevo


Just use ArrayList(resources.getStringArray( ... ).toMutableList()) instead.

If you don't need to use ArrayList exactly in your code, then you can change type of property to MutableList<String> and call resources.getStringArray( ... ).toMutableList()

Or you can use spread operator on your array and create ArrayList via call arrayListOf(*context.resources.getStringArray())

like image 5
hluhovskyi Avatar answered Oct 25 '22 21:10

hluhovskyi