功能展示:
2017-05-27-09mz02.gif
2017-05-27-09mz04.gif
2017-05-27-09mz03.gif
2017-05-27-08mz01.gif
功能要求:通过拼音或者号码搜索通讯录号码,并且高亮展示出来!
一
汉字和号码可以直接精确匹配这个很见简单不多说
二
拼音匹配相对复杂:
首先如果匹配肯定需要进行匹配的数据模型
数据模型.png
获取手机号码并进行数据设置:
···
public static ArrayList getContactData(Context context, ArrayList searchContactLists) {
//得到ContentResolver对象
ContentResolver cr = context.getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动光标
while (cursor.moveToNext()) {
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameFieldColumnIndex);
//取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//要获取所有的联系人,一个联系人会有多个号码
getContactById(cr, contactId, name, searchContactLists);
}
Collections.sort(searchContactLists, new SortByPinyin());//数据排序
return searchContactLists;
}
//根据通讯录ID
private static void getContactById(ContentResolver cr, String contactId, String name, ArrayList searchContactLists) {
if (!TextUtils.isEmpty(contactId)) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
if (null != phone) {
ContactsBean contact = new ContactsBean();
contact.setNam