Get item value in arrayadapter - android

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?

Related

how to extract id attribute value from string-array

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>

Best way of getting key & value from Spinner

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.

How to get the attribute "value" from the array

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

Android - Reading arrays.xml

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"));

Android Spinner: Get Value from Selection

I am using a spinner populating with an xml file:
<string-array name="Spinner Items">
<item>Item 1: 2.0 - 4.0</item>
<item>Item 2: 1.0 - 3.0</item>
Is it possible to set a name value pair like this:
<string-array name="Spinner Items">
<item name="Item 1: 2.0 - 4.0" value="2"></item>
<item name="Item 2: 1.0 - 3.0" value="1.5"></item>
So that I can populate the spinner with the name and then when an item is selected it returns the value of that specific item? I figure it must be easier to do all this in the xml file as opposed to have to write if statement for each item when the spinner is changed. Of course the real xml file is much longer than this snippet.
The only way to do this is to implement a custom adapter-class.
I did it wrong way, but it works.
My Spinner(spinnerSex) contains Male/Female choice:
<string-array name="spinnerSex">
<item>Male</item>
<item>Female</item>
</string-array>
java code:
Object objSelectedItem = spinnerSex.getSelectedItem();
String strSelectedItem = String.valueOf(objSelectedItem);
String[] arrvals = this.getResources().getStringArray(R.array.spinnerValues);
boolean isMale = (strSelectedItem.equals(arrvals[0]));

Categories

Resources