【发布时间】:2014-02-21 18:45:51
【问题描述】:
Android Studio 0.4.6
你好,
我有以下函数,它应该根据列名按 ASC 顺序获取数据库的行。但是它只是按照它们在数据库中列出的顺序获取行。
所以数据库中的名字应该按字母顺序排列。
我认为我的游标查询是正确的,因为在调试器中游标值是:
SQLiteQuery: SELECT _id, name, phone, email FROM friends ORDER BY name ASC
这是我想要的。然后我将光标设置到第一行并循环遍历它们,直到到达最后一行。
但是,它不会按名称的字母顺序显示。
private void loadDB() {
Cursor cursor = db.query(FriendContract.TABLE,
new String[] {FriendContract.Column.ID, FriendContract.Column.NAME, FriendContract.Column.PHONE, FriendContract.Column.EMAIL},
null, null, null, null, FriendContract.Column.NAME + " ASC");
/* Check if database is empty */
if(cursor.getCount() == 0) {
/* There are no rows to load - so just return */
Log.d(TAG, "loadDB() cursor.getCount() == 0. There are no rows, just just refresh listview");
adapter.notifyDataSetChanged();
return;
}
/* Clear all items from array list -
we are going to fill this with the content of the database */
friendsList.clear();
Friend friend;
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
friend = new Friend();
friend.setId(cursor.getLong(0));
friend.setName(cursor.getString(1));
friend.setPhone(cursor.getString(2));
friend.setEmail(cursor.getString(3));
/* Add this to the list of friends */
friendsList.add(friend);
cursor.moveToNext();
}
/* Clean up */
cursor.close();
/* Refresh the listview with the loaded friends */
adapter.notifyDataSetChanged();
}
【问题讨论】:
-
试着用小写顺序。因为这对我来说很完美
-
Order by 已生成,我只是将列和 ASC 传递给它。该参数用于'order by'
-
@user2009 你会得到你的结果
-
你确定它们输入数据库的顺序吗?我有一个按文本排序的表,为了让它工作,我必须使用以下选项创建它:CREATE TABLE IF NOT EXISTS Audits(_id INTEGER PRIMARY KEY, Name TEXT COLLATE NOCASE)
-
我的代码现在可以工作了。不知道发生了什么,也许是一些小故障。感谢您的建议。