问题
Can I count on string arrays within the "strings.xml" resource file to be parsed/deserialized in the same order every time?
If anyone can cite any documentation that clearly spells out this guarantee, I'd appreciate it. Or, at the very least, offer a significant amount of experience with this topic.
Also, is this a best practice or am I missing a simpler solution?
Note: This will be a small list, so I'm not looking to implement a more complicated database or custom XML solution unless I absolutely have to.
<!--KEYS (ALWAYS CORRESPONDS TO LIST BELOW ??)-->
<string-array name="keys">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<!--VALUES (ALWAYS CORRESPONDS TO LIST ABOVE ??)-->
<string-array name="values">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>
回答1:
Yes, as far as I'm aware you can assume that the order of items will be the same each time, meaning you can safely define key/value pairs using separately xml-declared arrays. Have a look at the API demos (e.g. the arrays.xml file) and you'll see that Google uses the same methodoly to specify static key/value pairs. More specifically, you'll be able to deduce this from entries_list_preference
and entryvalues_list_preference
. Actually, if you think about it: it would hardly make sense to offer entries
and entryValues
attributes for pointing to static resources for e.g. a ListPreference
if their order wouldn't be guaranteed.
Addendum: Multi-dimensional arrays in xml are not supported. You can however write your own xml parser to handle those cases, which actually isn't as hard as it may sound. It would probably take you more time though than simply defining two one-dimensional arrays.
来源:https://stackoverflow.com/questions/10039258/are-strings-xml-string-arrays-always-parsed-deserialized-in-the-same-order