如何从具有最大时间戳的 ContentProvider 获取完整行?

How to get complete row from ContentProvider with the bigest timestamp?

我有一个消息数据库:

message_id,
message_from,
message_to,
message_message,
message_time

如果我 select 消息我这样做例如:

public static List<MessageListEntry> selectMessages(Context context, String from, String to) {
    final String selection = DatabaseAdapter.COLUMN_MESSAGE_FROM + " = ? AND " + DatabaseAdapter.COLUMN_MESSAGE_TO + " = ?";
    final String[] selectionArgs = {from, to};

    final List<MessageListEntry> result = new ArrayList<>();
    final ContentResolver resolver = context.getContentResolver();
    final String[] projection = ContentProvider.AVAILABLE;

    Cursor cursor = resolver.query(ContentProvider.CONTENT_URI, projection, selection, selectionArgs, null);

    if (cursor != null && cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            // create new object and add it to result list
        }

        cursor.close();

        return result;
    } else {
        if (cursor != null) {
            cursor.close();
        }

        return result;
    }
}

但是如果我只想 select 从和到之间发送的最后一条消息,我该怎么办?

您必须按时间戳排序,并将限制设置为 1,如下所示:

Cursor cursor = resolver.query(ContentProvider.CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        COLUMN_MESSAGE_TIME + " DESC LIMIT 1");