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?

Mark Freeman
  • 543
  • 1
  • 6
  • 18

1 Answers1

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];
Community
  • 1
  • 1
Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • 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! – Mark Freeman Sep 28 '12 at 15:36
  • 1
    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 – Ian Warwick Sep 28 '12 at 15:39
  • Updated my answer to include a more specific solution, hope that helps! – Ian Warwick Sep 28 '12 at 15:42
  • 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. – Mark Freeman Sep 28 '12 at 15:48
  • Do you know if there is a way to get string array items by their name, not just the index? – AdamMc331 Jun 26 '15 at 01:58