Get data from android string array from resource - android

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.

Related

How can i create nested array in android studio Strings.xml

<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>

How can define two dimension string array in android resource?

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.

Is it possible to write string array literals in Android XML resource files?

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.

Use Java constants in arrays defined by XML

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.

Storing array into xml file

I want to store an array into a xml file.
The array will be called with the following function.
String getTextSpeech = TextSpeech[Index];
This is using the textToSpeechAPI.
How can I store the array TextSpeech[] in an xml file in the values folder and then call it within the class.
Thanks
Edit:
<resources>
<string-array name="myArray">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>
In activity you can call it as
String[] resourceString =getResources().getStringArray(R.array.myArray);
if your class is not of an activity type then use a contructor of class like
YourClass(Context activityContext)
{
String[] resourceString =activityContext.getResources().getStringArray(R.array.myArray);
}
You cant store any thing in values at run times but what you want can be achieved through various data storage solutions provided by android http://developer.android.com/guide/topics/data/data-storage.html
For your scenario I would suggest SQLite Databases

Categories

Resources