I have an array like this that defines the entry values for a ListPreference:
<string-array name="sortSelectionEntryValues">
<item>0</item>
<item>1</item>
</string-array>
Now instead of using 0 and 1 in XML, I want to use Java constants like e.g. ORDER_ASC and ORDER_DESC, because later I will access the value of the selected ListPreference entry programmatically and I have to check the value in code, but comparing the value to a well named constant makes code easier to read.
So what I want is something like that:
In Java:
class MyOrderClass
{
public static final String ORDER_ASC="0";
public static final String ORDER_DESC="1";
}
In the XML:
<string-array name="sortSelectionEntryValues">
<item>MyOrderClass.ORDER_ASC</item>
<item>MyOrderClass.ORDER_DESC</item>
</string-array>
Is that somehow possible? Thanks for any hint!
(Please note, the ordering is just a dumb example, I just need to know how to integrate Java constants into the XML definition of an array)
That's not possible. That said, you will never have to compare "0" to anything. You will likely use if(MyOrderClass.ORDER_ASC.equals(<selected item from list>){..} so it shouldn't be an issue.
You cant do it the way that you suggest, but...
You could probably do it a different way and define the constant in XML in a values file and define both your static in java, and your array in XML in terms of the defined value. This way it is only defined once and the same value is used.
Related
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.
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>
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.
For my PreferenceActivity, I am using the typical preferences.xml file.
The entryValues of a ListPreference is not something meant to be localized. Is it possible to define the entryValues attribute directly using some kind of array literal?
My first try below won't work, "pull, push" is considered a string:
<ListPreference
android:key="server_reception_mode"
android:title="#string/title_server_reception_mode_preference"
android:entries="#array/entries_server_reception_mode_preference"
android:entryValues="pull, push" />
You can declare a <string-array> array of strings. For example:
<string-array name="entry_values">
<item>pull</item>
<item>push</item>
</string-array>
You could then use it in XML:
<ListPreference
android:key="server_reception_mode"
android:title="#string/title_server_reception_mode_preference"
android:entries="#array/entries_server_reception_mode_preference"
android:entryValues="#array/entry_values" />
If all you're after is avoiding translation, you can use the convention used in the Android sources: they have resource files named values/donottranslate.xml, values/donottranslate-names.xml, etc.
Looking at the documentation for the entryValues attribute, no:
Must be a reference to another resource [...]
Of course, just because you have a string resource doesn't mean you have to localise it.
You'll need to create an array in your resources. You can't declare it directly like you were hoping to.
For those kind of arrays, I usually create a second XML file with all entries that do not need to be translated (like array-notranslate.xml). That way, if you hand your xml files to somebody for translation, they won't have to deal with it.
Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().