Well, it seems that this question could be answered in a simpler and more straightforward way... :-)
Simply put, Android allows you to attach a long
to any ListView
item, it's that simple. When the system notifies you of the user selection, you receive three identifying variables to tell you what was selected:
- a reference to the view itself,
- its numeric position in the list,
- this
long
you attached to the individual elements.
It's up to you to decide which of these three is the easiest for you to handle in your particular case but you have all three to choose from all the time. Think of this long
as a tag automatically attached to the item, only that it's even simpler and easier to read out.
The misunderstanding about what it usually does stems from a simple convention. All adapters have to provide a getItemId()
even if they don't actually use this third identification. So, by convention, those adapters (including many in samples in the SDK or all around the web) simply return position
for a single reason: it's always unique. Still, if an adapter returns position
, this really means it doesn't want to use this feature at all, since position
is already known, anyway.
So, if you need to return any other value you see fit, feel free to do so:
@Override
public long getItemId(int position) {
return data.get(position).Id;
}