public ArrayList<Chat> fetchChatHistory(String senderId, String receiverId) {
ArrayList<Chat> chatList = new ArrayList<Chat>();
String where = "" + DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN
+ " in (?,?) AND "
+ DataProviderConstants.MESSAGE_SENDER_ID_COLUMN + " in (?,?) "
/*
* +"("+DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN+"=? AND "
* +DataProviderConstants.MESSAGE_SENDER_ID_COLUMN+"=?)"
*/;
Cursor cursor = mContext.getContentResolver().query(
DataProviderConstants.MESSAGE_TABLE_CONTENTURI, null, where,
new String[] { senderId, receiverId, senderId, receiverId },
DataProviderConstants.MESSAGE_ID_COLUMN);
if (cursor != null && cursor.moveToFirst()) {
do {
Chat chat = new Chat();
chat.setRecieverID(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN)));
chat.setSenderID(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_SENDER_ID_COLUMN)));
chat.setMessage(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MSG_COLUMN)));
chat.setLat(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_LAT_COLUMN)));
chat.setLon(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_LON_COLUMN)));
int isunread = cursor.getInt(cursor
.getColumnIndex(DataProviderConstants.MSG_IS_UNREAD));
chat.setIsUnread((isunread == 0 ? false : true));
chat.setDelvired(true);
chatList.add(chat);
} while (cursor.moveToNext());
}
return chatList;
}
使用此代码可以向前方向从Sq-lite数据库获取数据,而我想要反向顺序我正在使用Collections.reverse但这里给出错误我不知道如何以反向顺序获取数据来自android中的Sq-lite有缺失有些东西请建议我或帮助我。
答案 0 :(得分:0)
在query
方法中,最后一个参数是&#34; orderby&#34;。变化
Cursor cursor = mContext.getContentResolver().query(
DataProviderConstants.MESSAGE_TABLE_CONTENTURI, null, where,
new String[] { senderId, receiverId, senderId, receiverId },
DataProviderConstants.MESSAGE_ID_COLUMN);
带
Cursor cursor = mContext.getContentResolver().query(
DataProviderConstants.MESSAGE_TABLE_CONTENTURI, null, where,
new String[] { senderId, receiverId, senderId, receiverId },
DataProviderConstants.MESSAGE_ID_COLUMN+" DESC");
这将为您提供按ID排序的行,但按降序排列。
答案 1 :(得分:0)
这样的事情会起作用吗?
public ArrayList<Chat> fetchChatHistory(String senderId, String receiverId) {
ArrayList<Chat> chatList = new ArrayList<Chat>();
String where = "" + DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN
+ " in (?,?) AND "
+ DataProviderConstants.MESSAGE_SENDER_ID_COLUMN + " in (?,?) "
/*
* +"("+DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN+"=? AND "
* +DataProviderConstants.MESSAGE_SENDER_ID_COLUMN+"=?)"
*/;
Cursor cursor = mContext.getContentResolver().query(
DataProviderConstants.MESSAGE_TABLE_CONTENTURI, null, where,
new String[] { senderId, receiverId, senderId, receiverId },
DataProviderConstants.MESSAGE_ID_COLUMN);
if (cursor != null && cursor.moveToLast()) {
do {
Chat chat = new Chat();
chat.setRecieverID(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_RECEIVER_ID_COLUMN)));
chat.setSenderID(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_SENDER_ID_COLUMN)));
chat.setMessage(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MSG_COLUMN)));
chat.setLat(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_LAT_COLUMN)));
chat.setLon(cursor.getString(cursor
.getColumnIndex(DataProviderConstants.MESSAGE_LON_COLUMN)));
int isunread = cursor.getInt(cursor
.getColumnIndex(DataProviderConstants.MSG_IS_UNREAD));
chat.setIsUnread((isunread == 0 ? false : true));
chat.setDelvired(true);
chatList.add(chat);
} while (cursor.moveToPrevious());
}
return chatList;
}
我已经改变了
cursor.moveToFirst()
到
cursor.moveToLast()
和
cursor.moveToNext()
到
cursor.moveToPrevious()
答案 2 :(得分:0)
有两种方式
a)使用答案中定义的DESC
b)反转数组chatList
。
就在你的行return chatList
;
添加此行
Collections.reverse(chatList );
return chatList ;
答案 3 :(得分:0)
getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Selection criteria
mSelectionArgs, // Selection criteria
mSortOrder);
指定ORDER BY ID DESC/ASC
答案 4 :(得分:-2)
只需使用原始查询。这与其他查询语言类似。
String selectQuery =“SELECT * FROM YOUR_TABLE_NAME由COLUMN_NAME DESC提供任何合作订单;”;
Cursor cursor = db.rawQuery(selectQuery,null);