Search code examples
androidandroid-cursoradapter

How to override the getItemId(int pos) method from CursorAdapter?


I am getting this question due to another answer on here, but didn't explain how to do what I am asking How to get the id of the row in onItemClick(ListView) when using a custom Adapter?

The answer which was accepted in that question is what I need since I am also making my own custom adapter (CursorAdapter), hence I will have the same problem. The problem is I have no idea how to accomplish that. I am looking at the Doc, and am not sure how to access the _id column from a cursor. Since the Doc doesn't have the constant which we can get that info from I'm stuck. Any help figuring it out would be much appreciated.

EDIT: I was not clear on what my question was, but just to clarify, like the title, how can I override the getItemId() method in the CursorAdapter custom class I created?


Solution

  • Assuming you don't have the Cursor as a member of your Adapter:

    @Override
    public long getItemId(int position) {
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);
        return cursor.getLong(mCursor.getColumnIndex("_id"));
    }