public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,null,null);
if (c != null) {
c.moveToFirst();
}
return c;
Cursor的代码:
Cursor cursor = myDb.getAllRows();
// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
startManagingCursor(cursor);
// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[]
{DBAdapter.KEY_KALO, DBAdapter.KEY_HYDRATE, DBAdapter.KEY_FAT, DBAdapter.KEY_PROTEIN ,DBAdapter.KEY_CAL};
int[] toViewIDs = new int[]
{R.id.item_Kalorien, R.id.item_KG_in_g, R.id.item_F_in_g, R.id.item_P_in_g,R.id.item_cal};
// Create adapter to may columns of the DB onto elemesnt in the UI.
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.item_tagebuch, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);
// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);
坦克你的帮助,抱歉我的英语不好:)
答案 0 :(得分:1)
“从后向”的陈述含糊不清我假设你想以特定的顺序阅读db
如果你想按字段的降序读取'foo'然后
String ORDER = "foo DESC";/*use ASC for ascending order*/
并更改鳕鱼如下
public Cursor getAllRows() {
String where = null;
String Order = "foo DESC";
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,Order,null);
if (c != null) {
c.moveToFirst();
}
return c;
以下是查询
的语法 public Cursor query (String table, String[] columns, String selection,String[]
selectionArgs, String groupBy, String having, String orderBy, String limit)
有关进一步说明,请参阅以下链接 SQLiteDatabase Android
答案 1 :(得分:0)
向后是一个奇怪的词,但我认为你需要对数据进行排序,所以这里是
1。在获取数据前排序
可以使用 ORDER BY 关键字来完成。 db.query()方法中的最后一个参数是order by子句(没有" order by"关键字)。
因此请将您的查询更改为
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,null,COLUMN_NAME + " ASC");
ASC用于按升序排序和DESC用于降序
如果你想要多种颜色,那么就这样做
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,null,COLUMN_NAME + " ASC, " +COLUMN_NAME2 + " DESC");
2. 获取数据后的排序
如果您在获取数据后尝试对其进行排序,则应在光标级别
完成Cursor cursor = myDb.getAllRows();
if (cursor.moveToFirst()) {
do {
// do what you need with the cursor here
} while (cursor.moveToNext());
}
OR REVERSE
if (cursor.moveToLast()) {
do {
// do what you need with the cursor here
} while (cursor.moveToPrevious());
}