Boardy Boardy - 4 months ago 63
Android Question

Set selected item of spinner programmatically

I am working on an android project and I am using a spinner which uses an array adapter which is populated from the database.

I can't find out how I can set the selected item programmatically from the list. For example if, in the spinner I have the following items:


  • Category 1

  • Category 2

  • Category 3



How would I programmatically make Category 2 the selected item when the screen is created. I was thinking it might be similar to c# I.E Spinner.SelectedText = "Category 2" but there doesn't seem to be any method similar to this for Android.

Answer

Use the following: spinnerObject.setSelection(INDEX_OF_CATEGORY2).