Sort a list in ascending order by date from sqlite

2019-04-19 15:47发布

问题:

I am having a code for retrieving data like this. I wanted to get records with the dates in the ascending order.I tried using "KEY_DATE_TIME ASC" . but it didnt work.

public Cursor fetchAllReminders() {

return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
        KEY_BODY, KEY_PHONE,KEY_DATE_TIME}, null, null, null, null, null);
}

回答1:

Assuming that KEY_DATE_TIME is a String constant holding the name of the db field, the following should work:

return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
        KEY_BODY, KEY_PHONE,KEY_DATE_TIME}, null, null, null, null, KEY_DATE_TIME + " ASC");