I am working on an android project and have a spinner which contains items from a string-array which is in the string.xml file.
In the strings.xml I have the following array
<string-array name="array_loginType">
<item>Select Login Type</item>
<item>Website</item>
<item>App</item>
<item>Other</item>
</string-array>
and the spinner contains the following XML
<Spinner android:id="@+id/add_cboLoginType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="@string/add_select_login_type"
android:padding="4dp"
android:entries="@array/array_loginType"/>
At some point the user can select the item from the spinner and when submitted it saves the item in the database. I am then allowing the user to edit the details and I am trying to set the selected item within the spinner based on the item that was retrieved from the database. I.e. if the saved item within the database says Website then Website will be selected inside the spinner.
Thanks for any help you can provide.
If you know what position in the array holds the correct selection, you can just use the
Spinner.setSelection();-method to set the spinner to display it.In your example, the
Websiteis found in position 1 of the array (1st actual entry is number 0).Therefore, your code should look something like this:
The second argument tells the spinner to “animate” the selection – so it actually displays the correct selection, and not just sets the correct value (if it’s set to false or not included at all, the spinner will have changed (so anything depending on the selection will work as intended) but it’ll still appear to be at the default selection).