2

I know this is a repost. I had to ask this question because I past question there is not a good solution or backdated answer. I comment there about my problem but no reply.

My question is simple. How can I get name value of string-array?

<string-array name="spinerArray">
    <item name="one">First</item>
    <item name="two">Second</item>
    <item name="three">Third</item>
    <item name="four">Fourth</item>
</string-array>

I can get First, Second, Third from above array. But I want one, two, three, fourth also.

There is two question on this.

getResourceEntryName for an array item

how to extract the name attribute from string array?

One accepted answer is

attributes.getValue("name");

But this is not working for me. Can anyone help me please.

Community
  • 1
  • 1
Xplosive
  • 711
  • 3
  • 14
  • 26
  • Why do you need the name? – user Apr 02 '14 at 18:46
  • 1
    I want to store some link and give them a title. I stored link as value and stored title in name. thats why i need name also. @Luksprog – Xplosive Apr 02 '14 at 18:51
  • 1
    The most simple solution would be to have two mirroring arrays, one holding the actual data and the other holding the names. You'll then just use the index to get the name or/and value. The common sense solution would be an xml layout in the `xml` folder that you parse at your own discretion(suitable if the list would be bigger and changed more frequently). – user Apr 02 '14 at 19:29
  • 1
    Mirror array creats hamper when you change an element. There is always a scope to mistake. @Luksprog – Xplosive Apr 02 '14 at 19:33

1 Answers1

0

Even in 2022, this does not seem to be a supported use case. One answer that I found that seems to work for simple use cases is to make a second string-array that holds onto the values. I agree with your concern that mirror arrays create a scope for mistakes, but I feel that can be mildly mitigated by keeping all of the resources together in the same file.

<resources>
    <string-array name="spinnerArray">
        <item>@string/First</item>
        <item>@string/Second</item>
        <item>@string/Third</item>
        <item>@string/Fourth</item>
    </string-array>
    <string-array name="spinnerArrayValues">
        <item>one</item>
        <item>two</item>
        <item>three</item>
        <item>four</item>
    </string-array>
</resources>

And then in your code :

// access the value from the spinner
val spinner : Spinner = view.findViewById(R.id.spinner)
val spinnerIndex = spinner.selectedItemPosition

// map the selected index to its value
val res = view.resources
val spinnerValue = res.getStringArray(R.array.spinnerArrayValues)[spinnerIndex]

println("${spinnerValue} was selected")

This isn't a good solution when you start needing multiple attributes per element, but I found this is a good way to map string resources to a workable value.

If you have multiple attributes, consider checking out this answer and this answer where they recommend parsing the xml or json files directly.

Kylaaa
  • 6,349
  • 2
  • 16
  • 27