Android 使用光标按列名排序
你好, 我有以下函数,它应该根据列名按ASC顺序获取数据库的行。但是,它只是按照行在数据库中列出的顺序获取这些行 所以数据库中的名字应该按字母顺序排列 我认为我的游标查询是正确的,因为在调试器中,游标值是:Android 使用光标按列名排序,android,sqlite,cursor,Android,Sqlite,Cursor,你好, 我有以下函数,它应该根据列名按ASC顺序获取数据库的行。但是,它只是按照行在数据库中列出的顺序获取这些行 所以数据库中的名字应该按字母顺序排列 我认为我的游标查询是正确的,因为在调试器中,游标值是: Android Studio 0.4.6 这就是我想要的。然后,我将光标设置到第一行,并在其上循环,直到到达最后一行 但是,它不会按名称的字母顺序显示 SQLiteQuery: SELECT _id, name, phone, email FROM friends ORDER BY nam
Android Studio 0.4.6
这就是我想要的。然后,我将光标设置到第一行,并在其上循环,直到到达最后一行
但是,它不会按名称的字母顺序显示
SQLiteQuery: SELECT _id, name, phone, email FROM friends ORDER BY name ASC
我上面问题的代码工作正常。试着用小字体编写order by。因为这在我的案例中工作得很好,所以生成了Order by,我只是将其传递给列和ASC。该参数适用于@user2009的“order by”@user2009。您将获得您的结果。您确定这是他们输入数据库的顺序吗?我有一个按文本排序的表,为了让它工作,我必须使用以下选项创建它:createtable IF NOT EXISTS Audits\u id INTEGER主键,Name text COLLATE nocase我的代码正在工作。不确定发生了什么,可能是出了点小问题。谢谢你的建议。
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();
}