Stack Overflow archive
1 score

get id of an item in spinner

score
1
question views
200
license
CC BY-SA 3.0

Make a separate integer-array in XML for the ids:

xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <integer-array name="ben_country_ids">
          <item>103</item>
          <item>210</item>
          <item>235</item>
          <item>76</item>
          <item>216</item>
          <item>200</item>
          <item>234</item>
     </integer-array>
</resources>

Get the int array in code:

java
final int[] benCountryIds = getResources().getIntArray(R.array.ben_country_ids);

Using the position get the correct id from the array in onItemSelected:

java
int countryId = benCountryIds[position];

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.