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.
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.
This might look like a stupid question but I actually have objects - let's call the object "Cocktail" - that contains a certain amount of fields such as cocktail name, list of ingredients, recipe, etc...
So basically what I would like to do is have my cocktails list available when my application needs it, but I do not really know how to store them. I thought of declaring all cocktails in Android string arrays such as following :
<
?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="first_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
<string-array
name="second_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
</resources>
And so on, but problem is I don't have the possibility of declaring item key so retrieve all the info will be a bit tedious, for example if the number of ingredients vary between different cocktails index retrieving will not work...
I'm not a big fan of hard-coding all my cocktail objects directly in code when app starts either so I don't really know what to do with that...
Any idea ?
Thanks !
I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?
Strings.xml isn't the right place. You COULD define the URLs, one at a time, in strings.xml, like this:
<string name="url1">http://foo1.com</string>
<string name="url2">http://foo2.com</string>
// etc
And then create an array at runtime, dumping all these values in one-by-one. But that would suck.
Instead, create an xml file in res/values/, name it whatever you want (for instance, urlvals.xml), and populate it like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="urls">
<item>http://foo1.com</item>
<item>http://foo2.com</item>
</array>
</resources>
Then, in code, reference it in the following way:
String[] myUrls = getResources().getStringArray(R.array.urls);
If you want an array of string resources, you can either:
1) add the strings in strings.xml (as per #Alexander's answer), and reference them by name in an array in values/yourresourcename.xml as per here.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myLinks">
<item>#string/link1</item>
<item>#string/link2</item>
</array>
2) add them directly the string array (verbatim) like #Alexander suggests
How you associate them with the elements in the ListView is up to you. I would suggest (if the content is static) creating a matching string array for (e.g.) the string you want to actually display (as opposed to the link itself).
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.