我一直在研究一个代码块,让用户按姓名、电子邮件或电话号码搜索(使用自动完成文本视图逐个字符)联系人。我已经制定了以下代码:
// General contact data, so we have to get the DATA1 attribute and use MIMETYPE
// to figure out what it is. Usually we'd query, say, ContactsContract.CommonDataKinds.Email.CONTENT_URI
Uri uri = ContactsContract.Data.CONTENT_URI;
// Limit the query results to only the columns we need for faster operations.
// Using a projection also seems to make the query DISTINCT
String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Data.DATA1,
ContactsContract.Data.MIMETYPE};
// Find contact records with an email address or phone number
// Search the name and data1 field (which may contain an email or phone number)
// for user-entered search phrase
String filter = "(" + ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?)"
+ " AND (" + ContactsContract.Data.DATA1 + " LIKE ? OR " + ContactsContract.Data.DISPLAY_NAME + " LIKE ?)";
String wildcardedConstraint = "%" + constraintString + "%";
String[] filterParams = new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, wildcardedConstraint, wildcardedConstraint};
// Sort contacts with the most recently contacted ones first. That's often 0 (unset)
// so do a sub-sort by last updated date, most recent contacts first
String orderBy = ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC, " + ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP + " DESC";
Cursor cursor = getContext().getContentResolver().query(uri, projection, filter, filterParams, orderBy);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String data1 = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
String mimetype = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
String number = null;
String email = null;
if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
email = data1;
} else if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
number = data1;
}
items.add(new Person(name, number, email));
Log.e("temp", name + " " + data1 + " " + mimetype);
}
cursor.close();
}
但是,电话号码搜索存在问题。在联系人中,电话号码采用许多不同的格式:
- +101234567890
- (123) 456-7890
- 1234567890
- 123-456-7890
等等。
如何调整我的联系人查询筛选器,以便用户的输入将找到任何格式的电话号码 - 最好不要使整个查询非常慢?
我发现的一些解决方案依赖于编辑表数据来标准化电话号码,这不是联系人的选项。也许那个规范化的数字字段会起作用...如果我能找到一种方法轻松地将其构建到联系人数据表上的此查询中。我知道我可以对每条记录进行额外的电话号码搜索,或者使用 Java 进行检查,但我认为这会让它变得非常慢。也许是查询中的正则表达式SQL运算符 - 但我不知道如何使其适用于用户的逐字符搜索,他们可能只输入了电话号码的一部分。
有什么想法吗?
Android内置的SQLite函数PHONE_NUMBERS_EQUAL
来执行此操作,该函数比较两个数字,如果它们足够相同,则返回1
,以达到来电显示目的。
您只需按如下方式更改filter
:
String filter = "(" + ContactsContract.Data.MIMETYPE + "=? OR "
+ ContactsContract.Data.MIMETYPE + "=?) AND "
+ "(PHONE_NUMBERS_EQUAL(" + ContactsContract.Data.DATA1 + ", ?, 0) OR "
+ ContactsContract.Data.DATA1 + " LIKE ? OR "
+ ContactsContract.Data.DISPLAY_NAME + " LIKE ?)";
并在您的filterParams
中添加另一个wildcardedConstraint
:
String[] filterParams = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
wildcardedConstraint,
wildcardedConstraint,
wildcardedConstraint };
PHONE_NUMBERS_EQUAL
函数中的最后一个INTEGER
参数指示是否使用严格数字比较; 1
的意思是严格,0
的意思是非严格。显然,这是一个可以从系统Resources
检索的系统范围的设置,但我不确定是什么因素决定了如何确定特定环境。上面的例子只是使用非严格比较。但是,如果是问题,可以像这样获得实际资源值:
private static final String STRICT_COMPARE = "config_use_strict_phone_number_comparation";
...
int strictResId = Resources.getSystem().getIdentifier(STRICT_COMPARE, "bool", "android");
boolean useStrict = Resources.getSystem().getBoolean(strictResId);