(Not specific to ListView, but to Adapter).
I keep implementing this when I subclass BaseAdapter:
@Override
public long getItemId(int position) {
return position;
}
Because have to implement that. I don't see any use of it, I need getItem(position) only, not getItemId(position).
I wonder if it has any significance (to Android SDK or something else)?
You have db table Notes with such 3 records:
+----+--------------------------+
| ID | Note Text |
+----+--------------------------+
| 43 | Note text blah blah |
| 67 | Note text blah blah blah |
| 85 | Last note |
+----+--------------------------+
and you implement an adapter to serve this data.
position - is an ordinal number of record position in the loaded dataset. For example if you load that table with ORDER BY ID ASC
, then
itemId - is a "primary key" of a record, and your implementation can return such values
In ArrayAdapter
and SimpleAdapter
position and itemId is the same thing:
public long getItemId(int position) {
return position;
}
In the SimpleCursorAdapter and all descendants of CursorAdapter itemId is a value from _id column:
public long getItemId(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIDColumn);
} else {
return 0;
}
} else {
return 0;
}
}