How to get the ID of selected item in a spinner?

Viewed 8985

I've a Spinner, with following values/properties.

<Spinner
    android:id="@+id/spin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:entries="@array/spin_ent"
    android:prompt="@string/spin_prompt" />

<string-array name="spin_ent">
    <item id="2">Two</item>
    <item id="1">One</item>
    <item id="3">Three</item>
    <item id="4">Four</item>
    <item id="5">Five</item>
</string-array>

From code level, I'm using the below code to get the ID of selected item.

final long spinID= ((Spinner)findViewById(R.id.spin)).getSelectedItemId();

If I select Two, I'm getting 0 instead of 2.

Why?

2 Answers
Related