Android通讯录模糊匹配,搜索实现高亮关键字(号码、首字母、
2017-05-29 本文已影响446人
HannyYeung
功能展示:
2017-05-27-09mz02.gif2017-05-27-09mz04.gif
2017-05-27-09mz03.gif 2017-05-27-08mz01.gif
功能要求:通过拼音或者号码搜索通讯录号码,并且高亮展示出来!
一
汉字和号码可以直接精确匹配这个很见简单不多说
二
拼音匹配相对复杂:
首先如果匹配肯定需要进行匹配的数据模型
获取手机号码并进行数据设置:
···
public static ArrayList<ContactsBean> getContactData(Context context, ArrayList<ContactsBean> 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<ContactsBean> 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.setName(name);
contact.setContactId(contactId);
if (!TextUtils.isEmpty(contact.getName())) {
getPinyinList(contact);
} else {
contact.setPinyinFirst("#");
}
while (phone.moveToNext()) {
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.getNumberList().add(phoneNumber);
}
searchContactLists.add(contact);
}
}
}
···
开始填充数据拼音:
private static void getPinyinList(ContactsBean contactsBean) {
StringBuffer bufferNamePiny = new StringBuffer();//NIHAO
StringBuffer bufferNameMatch = new StringBuffer();//NH
String name = contactsBean.getName();
for (int i = 0; i < name.length(); i++) {
StringBuffer bufferNamePer = new StringBuffer();
String namePer = name.charAt(i) + "";//名字的每个字
for (int j = 0; j < namePer.length(); j++) {
char character = namePer.charAt(j);
String pinCh = Pinyin.toPinyin(character).toUpperCase();
bufferNamePer.append(pinCh);
bufferNameMatch.append(pinCh.charAt(0));
bufferNamePiny.append(pinCh);
}
contactsBean.getNamePinyinList().add(bufferNamePer.toString());//单个名字集合
}
contactsBean.setNamePinYin(bufferNamePiny.toString());
contactsBean.setMatchPin(bufferNameMatch.toString());
String firstPinyin = contactsBean.getNamePinYin().charAt(0) + "";
if (indexStr.contains(firstPinyin)) {
contactsBean.setPinyinFirst(firstPinyin);
} else {
contactsBean.setPinyinFirst("#");
}
}
三
搜索时数据匹配
//通过拼音或者英文字母
private void findDataByEN(String inputStr) {
//把输入的内容变为大写
String searPinyin = Util.transformPinYin(inputStr);
//搜索字符串的长度
int searLength = searPinyin.length();
//搜索的第一个大写字母
String searPinyinFirst = searPinyin.charAt(0) + "";
for (int i = 0; i < searchContactLists.size(); i++) {
ContactsBean contactsBean = searchContactLists.get(i);
contactsBean.setMatchType(1);//字母匹配肯定是名字
//如果输入的每一个字母都和名字的首字母一样,那就可以匹配比如:你好,NH,输入nh就ok
if (contactsBean.getMatchPin().contains(searPinyin)) {
contactsBean.setHighlightedStart(contactsBean.getMatchPin().indexOf(searPinyin));
contactsBean.setHighlightedEnd(contactsBean.getHighlightedStart() + searLength);
contactLists.add(contactsBean);
} else {
boolean isMatch = false;
//先去匹配单个字,比如你好:NI,HAO.输入NI,肯定匹配第一个
for (int j = 0; j < contactsBean.getNamePinyinList().size(); j++) {
String namePinyinPer = contactsBean.getNamePinyinList().get(j);
if (!TextUtils.isEmpty(namePinyinPer) && namePinyinPer.startsWith(searPinyin)) {
//符合的话就是当前字匹配成功
contactsBean.setHighlightedStart(j);
contactsBean.setHighlightedEnd(j + 1);
contactLists.add(contactsBean);
isMatch = true;
break;
}
}
if (isMatch) {
continue;
}
//根据拼音包含来实现,比如你好:NIHAO,输入NIHA或者NIHAO。
if (!TextUtils.isEmpty(contactsBean.getNamePinYin()) && contactsBean.getNamePinYin().contains(searPinyin)) {
//这样的话就要从每个字的拼音开始匹配起
for (int j = 0; j < contactsBean.getNamePinyinList().size(); j++) {
StringBuilder sbMatch = new StringBuilder();
for (int k = j; k < contactsBean.getNamePinyinList().size(); k++) {
sbMatch.append(contactsBean.getNamePinyinList().get(k));
}
if (sbMatch.toString().startsWith(searPinyin)) {
//匹配成功
contactsBean.setHighlightedStart(j);
int length = 0;
//比如输入是NIH,或者NIHA,或者NIHAO,这些都可以匹配上,从而就可以通过NIHAO>=NIH,HIHA,NIHAO
for (int k = j; k < contactsBean.getNamePinyinList().size(); k++) {
length = length + contactsBean.getNamePinyinList().get(k).length();
if (length >= searLength) {
contactsBean.setHighlightedEnd(k + 1);
break;
}
}
isMatch = true;
contactLists.add(contactsBean);
}
}
}
if (isMatch) {
continue;
}
//最后一种情况比如:广发银行,输入GuangFY或者GuangFYH都可以匹配成功,这样的情况名字集合必须大于等于3
if (contactsBean.getNamePinyinList().size() > 2) {
for (int j = 0; j < contactsBean.getNamePinyinList().size(); j++) {
StringBuilder sbMatch0 = new StringBuilder();
sbMatch0.append(contactsBean.getNamePinyinList().get(j));
//只匹配到倒数第二个
if (j < contactsBean.getNamePinyinList().size() - 2) {
for (int k = j + 1; k < contactsBean.getMatchPin().length(); k++) {
//依次添加后面每个字的首字母
sbMatch0.append(contactsBean.getMatchPin().charAt(k));
if (sbMatch0.toString().equals(searPinyin)) {
contactsBean.setHighlightedStart(j);
contactsBean.setHighlightedEnd(k + 1);
contactLists.add(contactsBean);
isMatch = true;
break;
}
}
}
if (isMatch) {
//跳出循环已找到
break;
}
//sbMatch1是循环匹配对象比如GUANGFYH,GUANGFAYH,GUANGFAYINH,GUANGFAYINHANG,
//FAYH,YINH
StringBuilder sbMatch1 = new StringBuilder();
for (int k = 0; k <= j; k++) {//依次作为初始匹配的起点
sbMatch1.append(contactsBean.getNamePinyinList().get(k));
}
//只匹配到倒数第二个
if (j < contactsBean.getNamePinyinList().size() - 2) {
for (int k = j + 1; k < contactsBean.getMatchPin().length(); k++) {
//依次添加后面每个字的首字母
sbMatch1.append(contactsBean.getMatchPin().charAt(k));
if (sbMatch1.toString().equals(searPinyin)) {
contactsBean.setHighlightedStart(j);
contactsBean.setHighlightedEnd(k + 1);
contactLists.add(contactsBean);
isMatch = true;
break;
}
}
}
if (isMatch) {
//跳出循环已找到
break;
}
if (j >= contactsBean.getNamePinyinList().size() - 2) {
//如果说是剩余最后两个拼音不需要匹配了
break;
}
StringBuilder sbMatch2 = new StringBuilder();
sbMatch2.append(contactsBean.getNamePinyinList().get(j));
for (int k = j + 1; k < contactsBean.getNamePinyinList().size(); k++) {
sbMatch2.append(contactsBean.getNamePinyinList().get(k));
StringBuilder sbMatch3 = new StringBuilder();
sbMatch3.append(sbMatch2.toString());
//只匹配到倒数第二个
if (j < contactsBean.getNamePinyinList().size() - 2) {
for (int m = k + 1; m < contactsBean.getMatchPin().length(); m++) {
//依次添加后面每个字的首字母
sbMatch3.append(contactsBean.getMatchPin().charAt(m));
if (sbMatch3.toString().equals(searPinyin)) {
contactsBean.setHighlightedStart(j);
contactsBean.setHighlightedEnd(m + 1);
contactLists.add(contactsBean);
isMatch = true;
break;
}
}
}
if (isMatch) {
//跳出循环已找到
break;
}
}
if (isMatch) {
//跳出循环已找到
break;
}
}
}
}
}
}