我有一些游标可以从 Android 中检索联系人,一切正常,但问题是我无法使用“Order by”子句。我尝试了很多东西,甚至使用“ Upper
”和localized collection
子句但似乎不起作用,我使用哈希图来存储结果,所以我想我需要订购哈希图,但我不想要这个,因为 order by 应该解决问题并保存处理时间。这是我的代码:
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "' AND "+ ContactsContract.Contacts.HAS_PHONE_NUMBER +"='" +("1")+"'";
String sortOrder =" "+ ContactsContract.Contacts.DISPLAY_NAME +" ASC";
CursorLoader query = new CursorLoader(context,uri, projection,
selection, null,
sortOrder);
return query.loadInBackground();
}
这是按组获取联系人的其他方法:
contactInfo = new HashMap<String, ContactInfo>();
Cursor c = context.getApplicationContext().getContentResolver().query(ContactsContract.Data.CONTENT_URI
, new String[] {
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID,
ContactsContract.RawContacts.CONTACT_ID },
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ?"+ " AND "
+ ContactsContract.Contacts.HAS_PHONE_NUMBER +"='" +("1")+"'",
new String[] { groupId },"UPPER("+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
获得光标后,我使用 while(c.moveToNext())
代码