我有一个SearchView,如果我输入一个名字,它会显示一个与之匹配的自动填充列表。但是, Facebook 救护车和 AL Cricket 等联系人也会收到这些结果。如何排除此类结果并仅获取那些真实用户的联系人?
我用来获取显示名称的代码是这样的:
private String getDisplayNameForContact(Intent intent) {
Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
phoneCursor.moveToFirst();
int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = phoneCursor.getString(idDisplayName);
phoneCursor.close();
return name;
}
答案 0 :(得分:0)
您需要为contentResolver.query
添加选择和选择参数。
你想要的论点是:
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
String[] selectionArgs = {"1"};
你可以像这样添加它们:
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor contactsCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, selection, selectionArgs, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
常量ContactsContract.Contacts.IN_VISIBLE_GROUP
确定您要查询的联系人子组。
答案 1 :(得分:0)
如您所见,当您调用 - query(intent.getData(),null,null,null,null)时 - 获取所有列表数据。 Where为null,theare是您的选择参数。它仍然只是选择适当的选项。试试我的例子,并交换不同的值(ContactsContract.CommonDataKinds.SomethingElse)。
我希望这会有所帮助。
private static Cursor allContactsQuery(Context context) {
final String[] CONTACTS = new String[]{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.LOOKUP_KEY,
};
String SELECTION = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY +
"<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1" +
" AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
final String[] SELECTION_ARGS = null;
final String SORT_ORDER = ContactsContract.Contacts.SORT_KEY_PRIMARY;
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACTS,
SELECTION,
SELECTION_ARGS,
SORT_ORDER);
return cursor;
}