Dray
Dray

Reputation: 1954

Get item value in arrayadapter

I want to get the value of the selected item in the spinner. I am using array adapter.

<string-array name="my_list">
         <item value="">---Select the value from the List---</item>
    <item value="value1">data1</item>
    <item value="value2">data2</item>
    <item value="value3">data3</item>
    <item value="value4">data4</item>
    <item value="value5">data5</item>
</string-array>

If I select the "data1" in my spinner, I want to get the "value1"..not "data1"

Anyone help me. Quick response helps me a lot. thanks in advance.

Upvotes: 0

Views: 2568

Answers (2)

Sonam Daultani
Sonam Daultani

Reputation: 749

You have to add a values string-array like this instead of value attribute

<string-array name="my_list">
    <item value="">---Select the value from the List---</item>
    <item>data1</item>
    <item>data2</item>
    <item>data3</item>
    <item>data4</item>
    <item>data5</item>
</string-array>

<string-array name="my_list_values">
    <item value="">---Select the value from the List---</item>
    <item>value1</item>
    <item>value2</item>
    <item>value3</item>
    <item>value4</item>
    <item>value5</item>
</string-array>

To retrieve the values from the my_list_values, You need to write this in onItemSelected function in listener

String selectedValue = getResources().getStringArray(R.array.my_list_values)[parent.getSelectedItemPosition()];

You cann't do something like you have done directly as Android doesn't supports entryValues

Upvotes: 1

Slickelito
Slickelito

Reputation: 1786

I don't think that's possible, tried it myself a while back. Maybe you just do what I did and create a second array holding the values you want on the same index?

Upvotes: 1

Related Questions