2

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?

1
  • see Resources documentation. If that doesn't answer, i don't understand your question Commented Sep 28, 2012 at 15:20

1 Answer 1

3

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];
Sign up to request clarification or add additional context in comments.

5 Comments

This could work, thank you. I'll mark this as an accepted answer. It seems kind of messy having multiple arrays when there is already a value associated with each element of the array. Oh well. Thanks!
The example in the linked question does not actually work because entryValues is not a property of spinner, however you can look up the value using the selected index into the other array resource
Updated my answer to include a more specific solution, hope that helps!
I went with the option of having 2 arrays and using the index to get the value from the second array. It looks like that will work great. Good thinking! Thanks again.
Do you know if there is a way to get string array items by their name, not just the index?

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.