Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between getSelectedItemId and getSelectedItemPosition of spinner in android

please help me in following =>

what is difference between getSelectedItemId and getSelectedItemPosition of spinner

like image 722
sarwesh kumar Avatar asked Oct 21 '13 13:10

sarwesh kumar


People also ask

What are the two events of spinner control in Android?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

What is set spinner show () method?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.

Who is the parent class of spinner?

Android Spinner class is the subclass of AsbSpinner class.


1 Answers

I know this is very old, but for future reference here is what I found:

getSelectedItemPosition() works as you would expect and returns the position of the selected item in an array containing only the items. For example when the adapter is created with the following array data

["Alice", "Bob", "Carol", "Dave"]

and given Carol would be currently selected, this method would return 2, the index of Carol in the array.


Now for the more interesting part, getSelectedItemId(): This method can be used when the spinner was set up with for example a SimpleCursorAdapter, so the populated data is based on a cursor. This cursor enables you to have two columns, _id and someValue (the id column name must be _id, the second column can be called anything). Given the following cursor

+-----+-------+
| _id | name  |
+-----+-------+
|  55 | Alice |
|  67 | Bob   |
|  72 | Carol |
|  84 | Dave  |
+-----+-------+

and again given, that Carol is selected, this method will return 72, so the value of the selected rows '_id' column.

When the SpinnerAdapter is based on a "normal array", the id and position seem to be identical and both methods will return the same value.


I do not promise that this information is complete and 100% accurate, but is what I have found out through trial and error so far.

like image 162
petroni Avatar answered Nov 15 '22 01:11

petroni