(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)?
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.
Stable IDs allow the ListView to optimize for the case when items remain the same between notifyDataSetChanged calls. The IDs it refers to are the ones returned from getItemId .
Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
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;
}
}
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