Mark Freeman
Mark Freeman

Reputation: 541

Android: Is there a way to get the name value of a defined String-Array programatically?

I have an arrays.xml file containing quite a few string-array elements for use with Spinners in my app. I need to send a certain value for each selection with web service calls. The web service takes the database value represented by the description, not the description itself, so I set up the string-array like this:

<string-array name="collection_method">
    <item name="blank"></item>
    <item name="P">Prepaid</item>
    <item name="C">Collect</item>
    <item name="T">Third Party</item>
    <item name="D">Cash On Delivery</item>
</string-array>

My question is this: Is there a way to programatically get to the name value for the elements of the string-array?

Upvotes: 2

Views: 3275

Answers (1)

Ian Warwick
Ian Warwick

Reputation: 4784

If I understand correctly using a Spinner you can bind 2 arrays, one for entries and one for values, this similar question describes this method and alternatives to it:

Android - configure Spinner to use array

EDIT: Looking again at the linked question its not right, but you can still use dual arrays, you just have to get the selected index of the spinner and look up the value in the other array using:-

String value = getResources().getStringArray(R.id.value_array)[selectedIndex];

Upvotes: 3

Related Questions