0

I have this array in my xml

<string-array name = "locations">
    <item value = "KDLH">Duluth International Airport</item>
    <item value = "KBJI">Bemidji</item>
    <item value = "KAUM">Austin Municipal</item> 
    <item value = "KBDE">Baudette International Airport</item>
    <item value = "KBBB">Benson Municipal</item>  
    <item value = "KCBG">Cambridge Municipal</item>
    <item value = "KCQM">Cook Municipal Airport </item>
    <item value = "KCOQ">Cloquet</item> 
    <item value = "KTOB">Dodge Center Airport </item>
    <item value = "KEVM">Eveleth</item>
    <item value = "KLXL">Little Falls</item>
    <item value = "KMML">Marshall</item>
    <item value = "KANE">Minneapolis / Blaine </item> 
    <item value = "KLVN">Minneapolis, Airlake Airport</item>
    <item value = "KMSP">Minneapolis-St. Paul</item>
</string-array>

I'm trying to retrieve the value when an item is selected and not the text. How do I go about doing this? Currently I'm only able to retrieve the text by doing this. I can only currently retrieve the selected text and not the value attribute.

3
  • do you want to get value ? Commented Apr 18, 2013 at 5:00
  • @Rstar That's what he has mentioned :D Commented Apr 18, 2013 at 5:01
  • 1
    @user2293498, item tag has no value attribute Commented Apr 18, 2013 at 5:04

2 Answers 2

4

There's not really a great solution for this. Here's how I normally do it.

<string-array name="locations">
    <item>Duluth International Airport</item>
    <item>Bemidji</item>
    <item>Austin Municipal</item>
    <item>Baudette International Airport</item>
    <item>Benson Municipal</item>
    <item>Cambridge Municipal</item>
    <item>Cook Municipal Airport </item>
    <item>Cloquet</item>
    <item>Dodge Center Airport </item>
    <item>Eveleth</item>
    <item>Little Falls</item>
    <item>Marshall</item>
    <item>Minneapolis / Blaine </item>
    <item>Minneapolis, Airlake Airport</item>
    <item>Minneapolis-St. Paul</item>
</string-array>

<string-array name="location_values">
    <item>KDLH</item>
    <item>KBJI</item>
    <item>KAUM</item>
    <item>KBDE</item>
    <item>KBBB</item>
    <item>KCBG</item>
    <item>KCQM</item>
    <item>KCOQ</item>
    <item>KTOB</item>
    <item>KEVM</item>
    <item>KLXL</item>
    <item>KMML</item>
    <item>KANE</item>
    <item>KLVN</item>
    <item>KMSP</item>
</string-array>

Then you can just pull the value from the location-values array.

Sign up to request clarification or add additional context in comments.

Comments

1

Although I like McCracken's answer better, you COULD store each pair with some special formatting that you could later parse.

Example:

<string-array name = "locations">
    <item>KDLH:Duluth International Airport</item>
    <item>KBJI:Bemidji</item>
    ...
</string-array>

And then:

String location = context.getResources().getStringArray(R.array.locations)[index];
String[] pieces = location.split(":");
String code = pieces[0]; // KDLH
String name = pieces[1]; // Duluth International Airport

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.