what is difference between getSelectedItemId and getSelectedItemPosition of spinner in android
Asked Answered
H

1

6

please help me in following =>

what is difference between getSelectedItemId and getSelectedItemPosition of spinner

Hemialgia answered 21/10, 2013 at 13:46 Comment(1)
An item in the spinner has it's own id, this is not the position in the spinners data set. The position is the location in the data set.Mannerism
N
9

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.

Niggard answered 6/8, 2018 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.