【发布时间】:2017-06-11 14:25:59
【问题描述】:
有多种方法可以按显示名称查找联系人。例如这个答案Android - Find a contact by display name
但我需要找到模糊匹配的联系人。例如如果找不到“Kim”,我需要返回名为“Keem”的联系人。
我该怎么做?
【问题讨论】:
标签: android android-contacts android-contentresolver
有多种方法可以按显示名称查找联系人。例如这个答案Android - Find a contact by display name
但我需要找到模糊匹配的联系人。例如如果找不到“Kim”,我需要返回名为“Keem”的联系人。
我该怎么做?
【问题讨论】:
标签: android android-contacts android-contentresolver
没有可以对显示名称进行模糊搜索的构建 API,但您可以自己做,应该不难:
第一步,代码如下:
Map<String, Long> contacts = new HashMap<String, Long>();
String[] projection = {Contacts._ID, Contacts.DISPLAY_NAME};
// use null if you want to include hidden contacts
String selection = Contacts.IN_VISIBLE_GROUP + "=1";
Cursor cur = cr.query(Contacts.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
contacts.put(name, id);
}
if (cur != null) {
cur.close();
}
对于第 2 步,您可以使用 Jaro Winkler 或其他一些字符串距离算法,这里有一个库可以帮助您: https://github.com/tdebatty/java-string-similarity
【讨论】: