I use below code in my array.xml file inorder to defining a array:
<string-array name="cities_azarbayejanSharghi">
<item id="15">A</item>
<item id="36">B</item>
<item id="140">C</item>
<item id="95">D</item>
</string-array>
I want to access the id of each field in the code when it is selected as spinner's selecteditem. Is there any ways?
TypedArray ta = getResources().obtainTypedArray(R.array.your_array);
int resId = ta.getResourceId(arg_positionItem, 0);
You could be better off referencing other strings, such as the answer outlined here:
<string name="15">A</string>
<string name="36">B</string>
<string-array name="cities_azarbayejanSharghi">
<item>#string/15</item>
<item>#string/36</item>
</string-array>
Related
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.
In android arrays.xml, I can define arrays like below,
<string-array name="feed_icons">
<item value="0">#drawable/latest</item>
<item value="1">#drawable/video</item>
<item value="2">#drawable/world</item>
<item value="3">#drawable/sports</item>
<item value="4">#drawable/arts</item>
<item value="5">#drawable/dining</item>
</string-array>
I wonder how to get the drawable according the value? Is three any function? I just kown that,
String[] iconArrays = getResource().getStringArrays(R.arrays.feed_icons);
but it's not my desired.
You could save the drawable names in an array like:
<string-array name="feed_icons">
<item value="0">latest</item>
<item value="1">video</item>
</string-array>
Then fetch the drawable:
String[] iconArrays = getResources().getStringArray(R.arrays.feed_icons);
// Get specific drawable resource id
int drawableResId = this.getResources().getIdentifier(iconArrays[0], "drawable", getPackageName());
// Fetch drawable by resource id
Drawable yourDrawable = getResources().getDrawable(drawableResId);
// Use drawable
imageView.setDrawable(yourDrawable);
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);
I want to get the value of the selected item in the spinner. I am using array adapter.
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item value="value1">data1</item>
<item value="value2">data2</item>
<item value="value3">data3</item>
<item value="value4">data4</item>
<item value="value5">data5</item>
</string-array>
If I select the "data1" in my spinner, I want to get the "value1"..not "data1"
Anyone help me. Quick response helps me a lot. thanks in advance.
You have to add a values string-array like this instead of value attribute
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item>data1</item>
<item>data2</item>
<item>data3</item>
<item>data4</item>
<item>data5</item>
</string-array>
<string-array name="my_list_values">
<item value="">---Select the value from the List---</item>
<item>value1</item>
<item>value2</item>
<item>value3</item>
<item>value4</item>
<item>value5</item>
</string-array>
To retrieve the values from the my_list_values, You need to write this in onItemSelected function in listener
String selectedValue = getResources().getStringArray(R.array.my_list_values)[parent.getSelectedItemPosition()];
You cann't do something like you have done directly as Android doesn't supports entryValues
I don't think that's possible, tried it myself a while back.
Maybe you just do what I did and create a second array holding the values you want on the same index?
I have a set of preferences stored in arrays.xml, which looks similar to this:
<string-array name="firstname">
<item name="1">Bob</item>
<item name="6">Kevin</item>
<item name="3">Peter</item>
<item name="4">Paul</item>
<item name="5">Simon</item>
<item name="2">Matt</item>
</string-array>
<string-array name="FirstnameValues">
<item name="1">1</item>
<item name="2">2</item>
<item name="3">3</item>
<item name="4">4</item>
<item name="5">5</item>
<item name="6">6</item>
</string-array>
These are used to populate ListPreference. The item stored in SharedPreferences is the name of the item; for example, for Kevin the items stored in SharedPreferences would be 6.
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String Firstname = prefs.getString("namesList", "");
So here, Firstname would give me 6, when selecting the SharedPreference key namesList for the selected item Kevin.
All I want to do is read the array.xml, so I can relate the number 6 back to Kevin. I can use getResources().getStringArray(R.array.testArray); but this just returns an array containing "Bob, Kevin, Peter, Paul, Simon, Matt."
It does not include the named value.
Do you have any idea on how I can get the name as well as the item for each?
Well, I don't know if you can do it directly, but you could always create a wrapper class that loads both string arrays and gives you both values for any given array value.
When you define you preferences in your settings.xml you can access the arrays.xml as follows (applogies for typos)
<ListPreference
android:title="FirstNameValues"
android:summary="First Names"
android:key="fistNamePreference"
android:defaultValue="1"
android:entries="#array/firstName"
android:entryValues="#array/fistNameValues" />
In your activity onCreate add
addPreferencesFromResource(R.xml.settings);
If you need more clarification please let me know. Here is an example.
Arrays.xml
<string-array name="timeIntervalList">
<item>5 seconds</item>
<item>10 seconds</item>
<item>30 seconds</item>
<item>1 minute</item>
<item>5 minutes</item>
<item>10 minutes</item>
</string-array>
<string-array name="timeIntevalValues">
<item>5000</item>
<item>10000</item>
<item>30000</item>
<item>60000</item>
<item>300000</item>
<item>600000</item>
</string-array>
settings.xml
<ListPreference
android:title="Retry Interval"
android:summary="Time interval"
android:key="retryPeriodPref"
android:defaultValue="5000"
android:entries="#array/timeIntervalList"
android:entryValues="#array/timeIntevalValues" />
settingsAcitivty
in onCreate call
addPreferencesFromResource(R.xml.settings);
OtherAcitivity where you wish to use the preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
timerPeriod = Integer.parseInt(prefs.getString("retryPeriodPref", "5000"));