18

Begin new project in Kotlin and missing those.

Try to get string-array recources but can't.

In strings.xml I palced next items.

<string-array name="themeList">
    <item>white</item>
    <item>sepia</item>
    <item>black</item>
    <item>pink</item>
</string-array>

In code I try next:

val res: Resources = resources
val appThemeList = arrayOf(res.getStringArray(R.array.themeList))

for (value in appThemeList) {
    Log.i ("value", value.toString())
}

But in logCat i see:

I/value: [Ljava.lang.String;@40145f2

And I don'r understand, what I do incorrectly.

1
  • 1
    I used this concept as follows successfully: private var list= mutableListOf<String>("") list = resources.getStringArray(R.array.string-array-list-name).toList() as MutableList<String> Commented Apr 12, 2023 at 13:42

8 Answers 8

27

replace

val appThemeList = arrayOf(res.getStringArray(R.array.themeList))

to

val appThemeList = res.getStringArray(R.array.themeList)

In other case you got array

 val myArray = res.getStringArray(R.array.themeList) //already array

And added to another array

 arrayOf(myArray) // array of arrays
Sign up to request clarification or add additional context in comments.

Comments

9

In android is depend on context when outside Activity like this

val themes = context.resources.getStringArray(R.array.themeList)

or without context is direct to resource when inside Activity

val themes = resources.getStringArray(R.array.themeList)

Comments

3

As we know res.getStringArray return arraylist so you do not need to write arrayOf on your code.

Simple way to achieve your goal is:-

val list = res.getStringArray(R.array.list);

We can use arrayOf when we have to define our array or we have already arraylist like below :-

val myArray = arrayOf(4, 5, 7, 3);

Comments

2

Try this,

 val Lines = Arrays.asList(resources.getStringArray(R.array.list))

Comments

0

In kotlin use :

var yourVar = resources.getStringArray(R.array.your_string_array)

Comments

0

In kotlin, also need to use context like below

var arry = context.resources.getStringArray(R.array.your_string_array)

Comments

0

With this line of code you get exactly the element in the place of index.

val the_string = resources.getStringArray(R.array.YOUR_STRING_ARRAY)[index].toString()

This will be your res/values/strings.xml

<string-array name="YOUR_STRING_ARRAY">
    <item>kJ</item>
    <item>kWh</item>
    <item>Btu</item>
    <item>kcal</item>
</string-array>

As an example if the index is 1 then the return value is "kWh"

Comments

0

If you want use the path of resources in RecyclerView.Adapter Class you must put into function onBindViewHolder

val myItem = holder.itemView.resources.getStringArray(R.array.myItem_string_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.