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

血红的双手。 提交于 2019-12-04 08:28:13

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"));
}

I nkow this doesn't answer the question posed, but Sam took care of that. I thought I'd post this because there seems to be some confusion on the OPs part.

Following is an onListItemClick method from an activity that contains a list created with a custom cursor adapter:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Your code here
}

long id is the row id for the data contained in the row clicked (when the list is fed by a cursor adapter). No need to override getItemId.

You only need to override the getItemId (in my experience) if you do something like put information from different rows into a single line. As long as all your data for a list line is from the same row in the database, there's no need to go to that trouble.

I suppose another time you may need to use it would be if you took data from a cursor and put it into an array and then used an array adapter.. but that seems pretty roundabout...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!