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

原理如下,根据联系人号码获得相关联系人的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_key
				int 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;
	}

你可能感兴趣的:(根据联系人号码获取sort_key以实现按字母排序)