There are many methods for getting Key & Value from Array.
But what is best way getting Key & Value from an Array in arrays.xml like key => value via spinner?
arrays.xml:
<resources>
<string-array name="settings_listofitems">
<item name="1">item1</item>
<item name="2">item2</item>
</string-array>
</resources>
When i use code in below i get only item value. But i need item key value too:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems)[spinner1.getSelectedItemPosition()];
Any help is highly appreciated.
You can have Keys and Values both in separate Arrays, like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_listofitems_keys">
<item>1</item>
<item>2</item>
</string-array>
<string-array name="settings_listofitems_values">
<item>item1</item>
<item>item1</item>
</string-array>
</resources>
And then parse them like this :
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems_values)[spinner1.getSelectedItemPosition()];
String keyvalue = getResources().getStringArray(R.array.settings_listofitems_keys)[spinner1.getSelectedItemPosition()];
I hope this helps.
Related
I have ListPreference for selecting languages. The current trnalsations are defined in their own strings.xml as seen in the image. To fill the preference list i include the string-array elements inside the main strings.xml, making it a static configuration.
Question: Is there any way to make the Language ListPreference dynamic by iterating through all the strings.xml and extracting the Locale information from them some how on run time?
This is the current static setup:
values/strings/strings.xml
<resources>
<!-- Preferences -->
<string name="pref_language">Language</string>
<string-array name="pref_language_list">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values">
<item>en</item>
<item>se</item>
<item>de</item>
<item>fr</item>
<item>pt</item>
<item>es</item>
<item>ba</item>
</string-array>
</resources>
xml/pref_general.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:entries="#array/pref_language_list"
android:entryValues="#array/pref_language_values"
android:key="language"
android:title="#string/pref_language"
android:icon="#drawable/language"
/>
</PreferenceScreen>
entries and entryValues should work.
while I don't understand these useless string definitions & string assignments ...
add them directly into the arrays; into values/arrays.xml; with attribute translatable="false".
and these language tags also don't match the resource file locations.
<resources>
<string-array name="pref_language_list" translatable="false">
<item>English</item>
<item>Svenska</item>
<item>Deutsch</item>
<item>Français</item>
<item>Português</item>
<item>Español</item>
<item>Pусский</item>
</string-array>
<string-array name="pref_language_values" translatable="false">
<item>en</item>
<item>se-rSE</item>
<item>de-rDE</item>
<item>fr-rFR</item>
<item>pt-rPT</item>
<item>es-rES</item>
<item>ba-rRU</item>
</string-array>
</resources>
I want a drawable id array of integer values which I can store like an integer-array in res/values/XXX.xml by using integer-array tag. Below is integer-array declared in strings.xml
<integer-array name="icons">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</integer-array>
But I want to store drawable image ids like #drawable/someImage as an integer array in xml.
OR Is there any alternatives to store drawable integer ids as an integer array in xml.
I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:
First, integer-array in res/values/arrays.xml:
<integer-array name="frag_home_ids">
<item>#drawable/frag_home_credit_return_money</item>
<item>#drawable/frag_home_transfer</item>
<item>#drawable/frag_home_balance</item>
<item>#drawable/frag_home_charge</item>
<item>#drawable/frag_home_finance_cdd</item>
<item>#drawable/frag_home_finance_ybjr</item>
<item>#drawable/frag_home_more</item>
</integer-array>
Second, get resource integer values programmatically:
TypedArray tArray = getResources().obtainTypedArray(
R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller.
//After calling this function you must not ever touch the typed array again.
tArray.recycle();
Third, call the integer values like this:
holder.iv.setImageResource(ids[position]);
Of course, you can get integer values of string, color, integer, layout, menu...in this way.
I hope these codes will inspire you.
Take a look at documentation, specifically More Resource Types article. Quote:
Typed Array
A TypedArray defined in XML. You can use this to create an array of other resources, such as drawables.
EXAMPLE:
XML file saved at res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>#drawable/home</item>
<item>#drawable/settings</item>
<item>#drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
You can use a string array.
Extract:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="media_names">
<item>Big Buck Bunny</item>
<item>Elephants Dream</item>
<item>Sintel</item>
<item>Tears of Steel</item>
</string-array>
<string-array name="media_uris">
<item>http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4</item>
<item>http://archive.org/download/ElephantsDream_277/elephant_dreams_640_512kb.mp4</item>
<item>http://archive.org/download/Sintel/sintel-2048-stereo_512kb.mp4</item>
<item>http://archive.org/download/Tears-of-Steel/tears_of_steel_720p.mp4</item>
</string-array>
</resources>
What is it that you want to achieve, I cannot 100% tell you if this is the best choice for you.
I have a ArrayList in my strings.xml. I would like to choose elements from it and put it in a .setText(). How do I this?
Example: XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
String[] planets = getResources().getStringArray(R.array.planets_array);
Assigning the value to a textView:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(planets[1]); //Venus
Read more at:
http://developer.android.com/guide/topics/resources/string-resource.html
Should ArrayList be declared in arrays.xml?If so, use Context.getResource().getStringArray() to choose elements
I am trying to get the attribute "value" from my array in the xml file. This is my array:
<resources>
<string-array name="myArray">
<item name="item1" value="www.something.com" type="urls">first url</item>
<item name="item2" value="www.somethingelse.com" type="urls">second url</item>
</string-array>
</resources>
and basically I have a listview that should only display the name of the web page and in the intent, they will be redirected to the website. I am trying to get the value into a string.
Any help would be greatly appreciated.
Thankyou.
Based from your requirement, you can do it like this instead of reading the resource and read the file content manually/implementing the XML parser.
<resources>
<string-array name="array_url">
<item>www.something.com</item>
<item>www.somethingelse.com</item>
</string-array>
</resources>
<resources>
<string-array name="array_name">
<item>first url</item>
<item>second url</item>
</string-array>
</resources>
Then get the array with
String[] urls= getResources().getStringArray(R.array.array_url);
String[] names= getResources().getStringArray(R.array.array_name);
Use names for the list, and when it's clicked, you can get the item position, read the URL from urls.
Resources res = getResources();
String[] websites = res.getStringArray(R.array.myArray);
use this
String[] temp = getResources().getStringArray(R.array.myArray);
Try This...
String[] classes = getResources().getStringArray(R.array.myArray);
For example if I have the following String_Array
<string-array name="recipe_ingredients">
<item>#array/m1_ingredients</item>
<item>#array/m2_ingredients</item>
</string-array>
How can I access #array/m1_ingredients where it is another String_Array
EDIT :
No, every item of a StringArray must always be a String, of course. So it can't be another StringArray
check the Documentation on resources, scroll to StringArray
name
String. A name for the array. This name will be used as the resource ID to reference the array.
here's an eg
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);