quick question my SO/documentation-checking has failed to answer:
Is it possible to have the value of a string-array resource be a reference to another string-array resource?
For instance, say I have an array like this
<string-array
android:name="names">
<item>Danny</item>
<item>Trevor></item>
<item>Garth</item>
</string-array>
is there some way to get the effect implied by this:
<string-array
android:name="appellations"
android:value="#string-array/names" / >
such that, say,
getStringArray(R.array.appellations)
returns the "names" array?
Related
<resources>
<string name="app_name">UnConv</string>
<string-array name="mainunit">
<item>Area</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
</resources>
i want to add some other values to the above items like a subgroup. Example for area i want yard, acre etc how can i achieve that?
Unfortunately, There is no direct way to save two dimensional string-array in Android resource file. I provide 2 methods for replacement.
1. Save the data as Json string showing in the following code:
<string name="mainunit">{"Area":["yard","acre"],"Pressure":[],"Speed":[],"Volume":[]}</string>
Then parse Json String to Java/Kotlin Object.
2. Save subgroup value splitting with ",", and get value by position, but it has drawback because it ignored the key name.
<string-array name="mainunit">
<item>yard,acre</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
I know how to get data from string-array. I have done like this.
<string-array name="values">
<item>value1</item>
<item>value2</item>
</string-array>
String[] values = this.getResources().getStringArray(R.array.values);
But I dont know whether the below type string-array is possible and confused to get values from that.
<string-array name="country_data">
<item>
<country>Afghanistan</country>
<countryCode>93</countryCode>
<iso2>AF</iso2>
<iso3>AFG</iso3>
</item>
</string-array>
If it is possible please help me to get the values.
I dont know whether the below type string-array is possible
It is not.
If you want to have arbitrary XML, use res/xml/ for whatever XML structure you like. You can then use getResources().getXml() to get an XmlPullParser that you can use to parse that XML.
Or, wrap the contents of each of your <item> elements in CDATA, which should prevent the resource parser from messing with them. Then, each string you retrieve from the array would be plain XML, that you would need to parse.
Or, go with JSON in res/raw/ or assets/, using your favorite JSON parser (e.g., Gson).
Or, use SQLiteAssetHelper to package a database in your app that has this data pre-loaded.
Or, use R. Zagórski's solution, of having one <string-array> per object, with <item> elements for each field of the object.
xml :
<string-array name="_name">
<item>n11</item>
<item>n12</item>
<item>n13</item>
<item>n14</item>
<item>n15</item>
<item>n16</item>
</string-array>
Retrieve at java class:
String[] test;
test = getResources().getStringArray(R.array._name);
Of course it is possible.
In summary:
Define POJO representing your model
Create a function to import string array of arrays from resources into this POJO
Remember that you cannot use custom xml entries, they have to be <item>. And you need to know it's type upfront.
I wish to fill in a spinner control with MsgName field, and get MsgValue when I select a item of the spinner.
so I write the following code.but I don't think it's a good code, is there the better code?
Do I need to define two dimension string array for my app? How can I do? Thanks!
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<string-arrayname="MsgName">
<item>Inbox</item>
<item>Sent</item>
</string-array>
<string-arrayname="MsgValue">
<item>content://sms/inbox</item>
<item>content://sms/sent</item>
</string-array>
</resources>
I would want to explicitly associate each name with it's value, instead of just having them be in the same position in two separate arrays.
There's at least a couple of ways to accomplish this.
Use some sort of separator to concatenate the name and the value in the same item:
e.g.:
<string-array name="message">
<item>Inbox|content://sms/inbox</item>
<item>Sent|content://sms/sent</item>
</string-array>
Create a custom xml resource, e.g. res/values/message-list.xml:
<messages>
<message name="Inbox">content://inbox</message>
<message name="Sent">content://sent</message>
</messages>
Of course, either way, you're going to have process the contents and create a SpinnerAdapter - in the first case, you'll have to split the entries, and in the second case, you'll have to parse the xml.
I can make string to be a reference to another string just like that:
<resources>
<string name="my_string">My String</string>
<string name="my_alias">#string/my_string</string>
</resources>
And i tried to make string-array reference the same way:
<string-array name="my_string_array">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="my_string_array_alias">#array/my_string_array</string-array>
But this does not work, unfortunately. I tried to use this array and alias in ListPreference:
<ListPreference
android:key="my_key"
android:title="#string/my_title"
android:entries="#array/my_string_array_alias"
android:entryValues="#array/my_string_array"
android:dialogTitle="#string/my_dialog_title" />
and entries is just an empty array. Of course, i can make both entries and entryValues to take the same array, but i like it to be a different ones.
Found this on the Android dev site.
Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.
It's a little vague but I believe this means the framework doesn't support it.
Assuming I have a string array defined as:
<string-array name="settings">
<item>not very unique</item>
<item>not very unique</item>
<item>slightly more unique</item>
</string-array>
Is there a way to reference an item, by its index in the array, from a preferences XML?
I know how to reference the entire array:
android:entries="#array/settings"
But is there a way to reference only one item from the array?
Well no, there is no way, but you could do something like this:
<string name="mystring_1">awesome string is awesome</string>
<string name="mystring_2">even more awesome string</string>
<string-array name="mystrings">
<item>#string/mystring_1</item>
<item>#string/mystring_2</item>
</string-array>
So you basically reference another string in your resources, that is referenced in your string array. ;)