我使用ContentProvider和SQLiteQueryBuilder:
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(NEWS_TABLE);
switch(sUriMatcher.match(uri)){
case NEWS_ITEM_ID: qb.appendWhere(KEY_ID+ "=" + uri.getPathSegments().get(1));
break;
default: break;
}
String orderBy;
if (TextUtils.isEmpty(sortOrder)) {
orderBy = KEY_DATE;
} else {
/* ______ WANT TO REVERSE IT HERE _____ */
orderBy = sortOrder;
}
Cursor c = qb.query(newsDB,
projection,
selection, selectionArgs,
null, null,
orderBy);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
如何使用反向排序顺序进行查询?谢谢你的帮助。
答案 0 :(得分:15)
ORDER BY
子句可能有升序或降序。默认情况下使用升序。要设置反向排序顺序,您需要在DESC
子句的末尾添加ORDER BY
关键字。
if (TextUtils.isEmpty(sortOrder)) {
orderBy = KEY_DATE;
} else {
/* ______ WANT TO REVERSE IT HERE _____ */
orderBy = KEY_DATE + " DESC";
}