Home » Android » android – Can I give an ID to an item in String-array?

android – Can I give an ID to an item in String-array?

Posted by: admin June 15, 2020 Leave a comment

Questions:

My question is simple, yet I don’t know the answer.

WHAT I WANT:

<string-array name="items_array">
        <item id="id1">Item1</item>
</string-array>

THIS IS WHAT I CURRENTLY HAVE:

<string-array name="items_array"> 
   <item>Item1</item>
</string-array>

What do I want to achieve with that is, that I want to offer in my Spinner normal names (e.g. Martin). Yet in the ID of that item, I want to have e.g. “martin93”.

The ID is usefull to append it to my URL, from which I want to fetch some data. But I want the user to choice a “normal” name, instead of weird (for example FaceBook) url name.

IMPORTANT: The facebook example is just given for explanation purposes, I am trying to achieve something else, but very similar (with ID I want to fetch the real URL’s name, that is not nice to read, if I’d put it directly into Spinner).

Any ideas?

How to&Answers:

Typically you would have

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

    <string-array name="planets_array_values" translatable="false">
        <item>merc</item>
        <item>ven</item>
        <item>eart</item>
        <item>mars</item>
    </string-array>
</resources>

Then populate the spinner with planets_array, and

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    String value = getResources().getStringArray(R.array.planets_array_values)[pos];
}

Answer:

No, you can’t give ID to individual array items.
Here you may see syntax.
To be short this is example from mentioned docs:

<?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 this is how you get access to strings:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Updated
To achieve your goal you may store separated name and some additional info:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="names">
        <item>Martin:martin93</item>
        <item>Maria:mary1985</item>
  </string-array>
</resources>

And then just use String.split() method to get two (or more) parts.
Note that it may be necessary to choose more complicated separator than ‘:’.
And to use it directly in the Spinner you should implement your own adapter.

Answer:

If it is not needed to translate IDs you could map the translatable array entries to IDs in code. Advantage of that would be that you can avoid accidential order mismatches between IDs and values. For that use string resources for every array entry:

<string-array name="my_array" translatable="false">
    <item>@string/text_1</item>
    <item>@string/test_2</item>
    ...
</string-array>

<string name="text_1">My first entry</string>
<string name="text_2">My second entry</string>
...

In code retrieve the string resources and put them as key + ID into a hash map:

val myArrayIdMap: HashMap<String, String> = java.util.HashMap()
myArrayIdMap[getString(R.string.text_1)] = "id-1"
myArrayIdMap[getString(R.string.text_2)] = "id-2"
...

To get the ID of a specific string use the map, for example if the string array is used in a spinner, the select listener could retrieve the ID:

...
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    val selectedEntry: String = parent?.getItemAtPosition(position).toString()
    val id: String = myArrayIdMap.get(selectedEntry)
}