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
Related
Just a quickie,
i have an xml resource in res/values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="UserBases">
<item>2</item>
<item>8</item>
<item>10</item>
<item>16</item>
</integer-array>
</resources>
and ive tried several things to access it:
int[] bases = R.array.UserBases;
this just returns and int reference to UserBases not the array itself
int[] bases = Resources.getSystem().getIntArray(R.array.UserBases);
and this throws an exception back at me telling me the int reference R.array.UserBases points to nothing
what is the best way to access this array, push it into a nice base-type int[] and then possibly push any modifications back into the xml resource.
I've checked the android documentation but I haven't found anything terribly fruitful.
You need to use Resources to get the int array; however you're using the system resources, which only includes the standard Android resources (e.g., those accessible via android.R.array.*). To get your own resources, you need to access the Resources via one of your Contexts.
For example, all Activities are Contexts, so in an Activity you can do this:
Resources r = getResources();
int[] bases = r.getIntArray(R.array.UserBases);
This is why it's often useful to pass around Context; you'll need it to get a hold of your application's Resources.
get an Array from array.xml in resources of android project can be accessed.
from array.xml
<string-array name="weather_values">
<item>sunny</item>
<item>cloudy</item>
<item>rainy</item>
</string-array>
in Activity
String[] stringsArray = getApplicationContext().getResources().getStringArray(R.array.weather_values);
in Fragment
String[] stringsArray = getContext().getResources().getStringArray(R.array.weather_values);
for output in log
System.out.println("Array Values " + Arrays.toString(stringsArray));
output is
I/System.out: Array Values [sunny, cloudy, rainy]
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 have a string-array in my string.xml file in Android res/value folder like this
my string.xml
<string-array name="books_titles_en">
<item>Smoking Effeccts on Your Body</item>
<item>Smoking - effects on your body</item>
<item>How Tobacco Smoke Causes Disease</item>
<item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>
I need to retrieve the first element of array like this
my home.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string-array/book_title_en[0]"/>
I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android
Just to giva a full answer to your problem:
There is no way of accessing an array directly, but you could do something like this:
<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
<item>#string/books_titles_en_1</item>
<item>#string/books_titles_en_2</item>
<item>#string/books_titles_en_3</item>
<item>#string/books_titles_en_4</item>
</string-array>
and then:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string/book_title_en_1"/>
So you basically reference the neccessary strings directly, that are referenced in your string array. ;)
Refer to this link Android - retrieve string array from resources
And this link Help in getting String Array from arrays.xml file
Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.
The first link I posted used
for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}
which is a sample of how to obtain the whole array from the xml.
You can modify it for single array value retrieval, or whichever array value you wish to retrieve.
Happy coding :D
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 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