Search code examples
androidsqlitesimplecursoradapterandroid-cursoradapter

SQlite primary key field name & CursorAdapter subclasses


Why do CursorAdapter subclasses requires the primary key to be necessarily _id ?

Isn't there a method to override, or something like that, to change this behaviour ?

I have read this trick many times, and I am aware of that ... I just want to understand better ! Thanks


Solution

  • Why does CursorAdapter subclasses requires the primary key to be necessarily _id ?

    It turns around and provides that value in various places, such as the long id value in getView().

    Isn't there a method to override, or something like that, to change this behaviour ?

    No, sorry. If you do not have a suitable column, just add ROWID AS _ID to your list of columns to return in rawQuery():

    SELECT ROWID AS _id, foo, bar FROM really_important_table ORDER BY foo;
    

    ROWID is automatically added to all SQLite tables by default, and is a unique integer, which fits the _id requirements nicely.