根据联系人号码获取sort_key以实现按字母排序

来源:互联网 发布:手机网络监控直播 编辑:程序博客网 时间:2024/06/10 09:48

原理如下,根据联系人号码获得相关联系人的RAW_ID。

ContentResolver resolver = mContext.getContentResolver();// Phone里面的数据Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);if (phoneCursor != null) {while (phoneCursor.moveToNext()) {// 读取联系人号码int phoneNumberIndex = phoneCursor.getColumnIndex(Phone.NUMBER);String phoneNumber = phoneCursor.getString(phoneNumberIndex);if (TextUtils.isEmpty(phoneNumber))continue;int contactNameIndex = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME);String contactName = phoneCursor.getString(contactNameIndex);// 根据RAW_ID读取sort_keyint rawContactIdIndex = phoneCursor.getColumnIndex(Phone.CONTACT_ID);Long rawContactId = phoneCursor.getLong(rawContactIdIndex);String sortKey = getSortKeyString(rawContactId);}phoneCursor.close();}

再根据RAW_ID在raw_contacts表中查询到联系人的sort_key。

private String getSortKeyString(long rawContactId) {String Where = ContactsContract.RawContacts.CONTACT_ID + " ="+ rawContactId;String[] projection = { "sort_key" };Cursor cur = mContext.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, Where,null, null);int sortIndex = cur.getColumnIndex("sort_key");cur.moveToFirst();String sortValue = cur.getString(sortIndex);cur.close();return sortValue;}

原创粉丝点击