如何使用最佳时间戳从ContentProvider获取完整的行?

时间:2016-03-07 15:57:31

标签: android android-contentprovider

我有一个消息数据库:

message_id,
message_from,
message_to,
message_message,
message_time

如果我选择消息,我会这样做:

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

但是,如果我只想选择发往和发送的最后一条消息,我该怎么办?

1 个答案:

答案 0 :(得分:1)

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

ev = Event.create(:name "test1").save
rule = Rule.create.save