I am working with spinner in android for a while now, and I always use what it says in documentation and I would use parent.getItemAtPosition(pos)
to get the Item, or I would use questions like this but the question is: docs say the
id in
onItemSelected(AdapterView<?> parent, View view, int position, long id)
is the row id of the selected item
but is it!?
what does that mean?
if it is the Id of row which is selected how can I get or set it?
it is long
if it was the Id of a View
shouldn't it be int
?
thanks.
You have to understand how Adapters work in Android. Basically they hold a list of items. Usually an array (ArrayAdapter) or a list. That list has items in a fixed ordering. Item 1 is at index 0 and so on. But if this is list is bound to a ListView via the ArrayAdapter, then the ArrayAdapter is asked to create a view for each row that is visible on screen. Each row (view) is then initialized with the corresponding item.
But the items might be shown in a different ordering like alphabetical ascending. Therefore the position is the position of the item in the underlying array, which is fixed. While the rowId is the position within the list that is shown on screen. The number of rows in the ListView is usually much smaller than the underlying list handled by the ArrayAdapter. Such rows that are scrolled off-screen will be re-used for those rows that come into the screen. Such re-used row will be re-initialized by the new item from the array (see parameter convertView in ArrayAdapter.getView).
I hope this image explains it in more detail. Otherwise refer to the documentation or to this example.
Hope this helps.