我一直在处理一段代码,让用户通过姓名,电子邮件或电话号码搜索(使用AutoCompleteTextView逐个字符)联系人。我已经制定了以下代码:
// 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();
}
但是,电话号码搜索存在问题。在联系人中,电话号码有多种不同的格式:
等等。
如何调整我的联系人查询过滤器,以便用户的输入可以找到任何格式的电话号码 - 最好不要使整个查询速度极慢?
我发现的一些解决方案依赖于编辑表格数据来标准化电话号码,这不是联系人的选项。也许规范化的数字字段可以工作......如果我能找到一种方法在Contact数据表上轻松地将它构建到这个查询中。我知道我可以为每条记录进行额外的电话号码搜索,或者使用Java进行检查,但我认为这会使它变得非常慢。可能是查询中的正则表达式SQL运算符 - 但我不知道如何使用它的字符逐字符搜索,它们可能只输入了部分电话号码。
有什么想法吗?
答案 0 :(得分:1)
您可以使用Android的内置SQLite函数PHONE_NUMBERS_EQUAL
执行此操作,该函数会对两个数字进行比较,如果它们与查询者ID相同,则会返回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 ?)";
并向wildcardedConstraint
添加另一个filterParams
:
String[] filterParams = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
wildcardedConstraint,
wildcardedConstraint,
wildcardedConstraint };
INTEGER
函数中的最后PHONE_NUMBERS_EQUAL
参数表示是否使用严格的数字比较; 1
表示使用strict,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);