PLB
PLB

Reputation: 891

Android: sort spinner entries (feeded via XML string-array)

I'm pretty new to Android so forgive me if I might not use the correct "terminology".

I've been looking at spinners and found out that there are two ways to add the entries:

  1. in the .java file of my activity, using StringAdapters and so on
  2. in the xml, using the "android:entries" attribute and a string-array defined in the strings.xml file

I tried the first option and was able to sort the Spinner options (I retrieved the string-array, put it into an ArrayList and then used Collections.sort... might not be the simplest solution), but I would be rather interested in using the second option, because since I already defined all my strings, it seems like it's going to make my code lighter... plus, I'd like to know if there's a way of oding what I'm trying to do... I now have in strings.xml something like this:

<resources>
    <string name="competition_backstroke100">Backstroke 100</string>
    <string name="competition_butterfly50">Butterfly 50</string>
    <string name="competition_butterfly100">Butterfly 100</string>

    <string-array name="competitions">
    <item>@string/competition_butterfly100</item>
    <item>@string/competition_backstroke100</item>
    <item>@string/competition_butterfly50</item>
</string-array>
</resources>

and put android:entries="@array/competitions" in my xml spinner definition

Now my question: the entries are added in the same order as they appear in the competitions definition. I understand that I could just sort them manually, but this wouldn't work in case my app runs in a different language (the order would stay the same, I guess). Is there any way to sort my strings directly in the xml?

Upvotes: 0

Views: 249

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

Define different versions of the <string-array> resource as needed to affect the sort order, for translations that need a new sort order. IOW, if you translate <string> resources, you "translate" the <string-array> sort order as well.

Upvotes: 1

Related Questions