Search code examples
androidandroid-cursoradapter

Android Custom CursorAdapter - Get primary key in getView?


I've got some code in GetView in a custom CursorAdapter that requires the primary key, and would like to set a variable equal to it.

How do I do this efficiently? GetView takes in position, convertView, and a GridView parent as arguments, so I don't have the cursor there.


Solution

  • Ask your adapter to translate ListView line number position.

    I use code similar to this

    public class MyCursorAdapter extends CursorAdapter {
        ...
    
        /** internal helper. return null if position is not available */
        private Cursor getCursorAt(int position) {
            return (Cursor) getItem(position);
        }
    
        /** translates offset in adapter to id of image */
        public long getImageId(int position) {
            Cursor cursor = getCursorAt(position);
    
            return cursor.getLong(cursor.getColumnIndex(MY_LONG_COLUMN_NAME));
        }
    
    }