在android中按字母顺序获取联系人?

时间:2015-02-05 05:21:19

标签: android mysql

Cursor cursor = contentResolver.query(Data.CONTENT_URI, null, "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE+ "=?) AND " + Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts)", new String[] {Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE  }, Data.CONTACT_ID);

我想按字母顺序获取联系人,但此查询以非字母顺序提取它们。我不知道在此查询中附加asc订单的位置。请帮助

2 个答案:

答案 0 :(得分:2)

第五个参数是sortOrder。您已通过Data.CONTACT_ID

Cursor cursor = contentResolver.query(Data.CONTENT_URI, null, "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE+ "=?) AND " + Data.CONTACT_ID + " IN (SELECT " + Contacts._ID + " FROM contacts)", new String[] {Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE  }, Data.DISPLAY_NAME+" ASC");

答案 1 :(得分:0)

| * |在5个查询参数中添加以下行,就像Sort-Order一样。

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"

| * |优化:在一个查询中按字母顺序获取带有标签和照片缩略图的显示名称和编号:

ContentResolver contentResolver = getContentResolver();
Cursor contctKsrVar = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Phone._ID,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.LABEL,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI},
            null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");