Home > front end >  Array of large amount of integers in xml string file Kotlin android Studio
Array of large amount of integers in xml string file Kotlin android Studio

Time:02-05

I want to make a Spinner to select the age ofthe users of my app. I wanted to know how I could create an array of all integers between 0 and 10 in the string resource file .xml in android Studio.

This is what I thought I was going to do

<integer-array name="AgeArray">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>12</item>
    <item>13</item>
    <item>14</item>
    <item>15</item>
    <item>16</item>
    <item>17</item>
    <item>18</item>
    <item>19</item>
    <item>20</item>
</integer-array>

But I thought that there must be a more elegant, more efficient way of creating such an array, or am I wrong?

Thanks in advance

CodePudding user response:

You cannot write to a resource file (at runtime), resource files are read-only.

While developing you can create a scratch file in Android Studio, execute the following code and then copy-paste the result from the output panel to the resource file:

val list = (1..100).joinToString("\n") { "    <item>$it</item>" }
val xml = "<integer-array name=\"AgeArray\">\n$list\n</integer-array>\n"
println(xml)
  •  Tags:  
  • Related