0

In my Activity I need to get a group of values from string.xml and add them to my mutableList. It's inconvenient to retrieve them manually like this:

specArray.add(R.string.string1)
specArray.add(R.string.string2)
specArray.add(R.string.string3)
specArray.add(R.string.string4)
//...

I tried to wrap it in a loop, and I ended up with something like this:

var count = 0
var specArray = mutableListOf<String>()
val stringCustom = "specC"
var buffer = getString(R.string.stringCustom)
while(count <= 36) {
    specArray.add(buffer)
    buffer = getString(string)
    val specArray = mutableListOf<String>()
    count++
}

but obviously it doesn't work, because we cannot specify a path to the resource with stringCustom in it.

YektaDev
  • 438
  • 3
  • 11
  • 24
Baslim734
  • 1
  • 1

1 Answers1

1

In this case, you might want to use a String array:

<?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>

And the code to retrieve the items in Kotlin:

val array: Array = resources.getStringArray(R.array.planets_array)

Or Java:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
YektaDev
  • 438
  • 3
  • 11
  • 24