please help me in following =>
what is difference between getSelectedItemId
and getSelectedItemPosition
of spinner
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.
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.
Android Spinner class is the subclass of AsbSpinner class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With