I am storing some informaiton in my android app and per unit of info, I need to store multiple data for each item in string-array or what ever appropriate.
For example if we take a list of countries, the XML file could be defined as follows. Here we have only one value, which is the name of the country that is listed in a string-array.
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
My requirement is to save more info, such as the country name and the country code in one resource.
My intention is to iterate over a resource and then for each country, I be able to get its sub items such as country name and country code. This way I can save objects that I need in the application and generate them from parsing the file.
At the moment, I am considering a string split and get the values or by using 2 arrays, but wondering if there is any eligent way to do it. XML and relavent Java code is appreciated.
Thanks in advance.
<resources>
<string-array name="countries_array">
<item>
<name>Bahrain</name>
<code>12345</code>
</item>
...
</string-array>
</resources>
CountryInfo.java:
public class CountryInfo {
public String mName;
public String mCode;
public CountryInfo(String name, String code) {
mName = name;
mCode = code;
}
}
ArrayList<CountryInfo> countryList = new ArrayList<CountryInfo>();
Then parse the xml file with an XML parser(SAX or DOM, examples can be found here) and then you can add each country info into your array like this:
CountryInfo tmp = new CountryInfo(name, code);
countryList.add(tmp);
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>
In my little app, I have a long list of data. Before, I used an editor to let the user enter the data - but now I would like to put the data static in program code/string list value Folder.
What is the best way to achieve this?
If you already know the values inside array you can crate string array inside strings.xml file which is present under values.
example:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sports_array">
<item>Football</item>
<item>Cricket</item>
<item>Hockey</item>
<item>Tennis</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] sportlist = res.getStringArray(R.array.sports_array);
so now your sportlist will contain all the items specified in sports_array which is declared inside strings.xml
for more information pls see
http://developer.android.com/guide/topics/resources/string-resource.html
i want to get a string from strings.xml. i know how to do this. but my problem is something else:
i have a String Variable which changes every time, and every time it changes, i want to look at strings.xml and check if that String variable exists in Strings.xml, then get text.
for example:
String title="sample title" \\ which changes
String city= "sample city"
String s = getResources().getString(R.string.title);
in the third line: title is a String, and there isn't any "title" named String in Strings.xml
how can i do this? please help me
As far as I can tell, you could use public int getIdentifier (String name, String defType, String defPackage). Its use is discouraged, though.
To use it (I haven't done it but I had once read about the method) you probably would need to:
int identifier = getResources().getIdentifier ("title","string","your.package.name.here");
if (identifier!=0){
s=getResources().getString(identifier);
}
else{
s="";//or null or whatever
}
One thing you could do to get strings from dynamic keys is make 2 string arrays and put them in a HashMap.
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="title_keys">
<item>title1</item>
<item>title2</item>
<item>title3</item>
</string-array>
<string-array name="title_values">
<item>Real Title 1</item>
<item>Real Title 2</item>
<item>Real Title 3</item>
</string-array>
</resources>
And in your code:
String[] titleKeys = getResources().getStringArray(R.array.title_keys);
String[] titleValues = getResources().getStringArray(R.array.title_values);
HashMap<String, String> titles = new HashMap<String, String>();
for(int i = 0; i < titleKeys.length; i++) {
titles.put(titleKeys[i], titleValues[i]);
}
Finally, to get your titles from a dynamic key:
titles.get(titleFromSomewhere);
You wouldn't. You use strings.xml for constant strings. What you want to do is one of two things.
1)You want a String to be constant, but one of a few options (for example, one item in a list of countries). In this case, put all of the options in strings.xml, and hold which one you're currently using in an int. When you need to get the actual string, use getString().
2)It really can be any string (for example, a user entered name). In that case it doesn't go in strings.xml at all, you just use a String variable.
This can not be done, resources are converted to unique ints in R.java, and those are used to look up your actual string resources.
So R.string.title is actually something like 0x78E84A34.
You can write your own class which manages strings for you utilizing a HashMap<String,String> to lookup full strings for shorter "key" strings.
I have the following problem:
I created a Char Sequence and was able to name 4 units. I would however rather use strings from my XML file for localization purposes. Is there any way to achieve that?
final CharSequence[] choices =
//want to add strings here i.e. R.strings.lemonade
{"Coke", "Pepsi" , "Sprite" , "Seven Up" };
builderType.setSingleChoiceItems( choices, selected, new OnClickListener()
{.......
Error message:
Type mismatch: cannot convert from int to CharSequence
There's another overload of AlertDialog.Builder.setSingleChoiceItems() that takes in an int resource id for a string array of items. Put the following in an xml in res/values e.g. strings.xml:
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then you can use it as:
builderType.setSingleChoiceItems(R.array.choices, selected, new OnClickListener(), ...
For generic cases, you can also load string array resources with Resources.getStringArray() as suggested by #Egor.
Create a string-array in strings.xml
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then fetch it from resources
String[] choices = context.getResources().getStringArray(R.array.choices);
Then use it in setSingleChoiceItems() as is, since String implements CharSequence.
I am not exactly clear on how to implement ExpandableList as array resource instead of hard-coded array (by replacing an existing hard-coded array with XML layout). Here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">adaptor_ExpandableList1</string>
<string name="expandable_list_sample_action">Sample action</string>
<string-array name="Groups">
<item>People Names</item>
<item>Dog Names</item>
<item>Cat Names</item>
<item>Fish Names</item>
</string-array>
<string-array name="People Names">
<item>Arnold</item>
<item>Barry</item>
<item>Chuck</item>
<item>David</item>
</string-array>
<string-array name="Dog Names">
<item>Ace</item>
<item>Bandit</item>
<item>Cha-Cha</item>
<item>Deuce</item>
</string-array>
<string-array name="Cat Names">
<item>Fluffy</item>
<item>Snuggles</item>
</string-array>
<string-array name="Fish Names">
<item>Goldy</item>
<item>Bubbles</item>
</string-array>
</resources>
Here is what I changed in the Java file:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = getResources().getStringArray(R.array.Groups);
private String[][] children = {getResources().getStringArray(R.array.People Names),
getResources().getStringArray(R.array.Dog Names),
getResources().getStringArray(R.array.Cat Names),
getResources().getStringArray(R.array.Fish Names)
};
Of course R.array.xxx Names does not work. So how can I correct the problem when there are statements in R.java such as this: public static final int Cat Names=0x7f040003;.
There must be a rule for this but I don't know it yet.
Browsing through numerous examples, I think I also need to have group and child layout files and perhaps a main layout file (I think I know how to implement them if I need to). Is this correct?
Thanks in advance for your help!
you have space in the name of string array
remove space from "people names" and other string array names.