Android Question Is it possible to set the selected item in a spinner

Colin Evans

Active Member
Licensed User
Longtime User
Hi, whilst I use the spinner selection to navigate and retrieve data from my sql lite database, I have a search facility which works in retrieving the data but I need to set the Spinner.selecteditem to the Item searched on, is this possible?

All Items in the Spinner are text, i.e.
Item1
Item2
Item3
etc

so if I search for something out of the database with Item27 and it returns the data I need to set the spinner.SelectedItem to Item27 as the rest of my code, the buttons on screen work off the spinner.SelectedItem,

many thanks
 

Colin Evans

Active Member
Licensed User
Longtime User
Hi Mark, don't think that's possible as I wouldn't know the index number as SelectedIndex uses an integr I believe, unless you know otherwise
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If I understood correctly, something like this (untested) should work
B4X:
Public selectExistingItemInSpinner( mySpinner as Spinner, itemToSearch as String, addToSPinnerItemsIfNotPresent as Boolean )
   For k=0 to mySpinner.size-1
      If mySpinner.GetItem(k) = itemToSearch Then
         mySpinner.SelectedIndex = k
         return
      End if
      ' If you want to add this item to the spinner in case it doesn't already exist
      If addToSpinnerItemsIfNotPresent=True Then
         mySPinner.Add(itemToSearch)
         mySpinner.selectedIndex = mySPinner.size-1
      End if
   Next
End Sub
 
Upvote 0
Top