您好我正在使用Android联系人搜索模块。我正在查询下运行。
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
从这个查询我多次得到结果。我有什么事我做错了。我想要DISTINCT
结果集。
请帮帮我。
答案 0 :(得分:2)
我认为你的意思是你有一些联系人的重复记录。因此,您必须为查询添加条件
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
答案 1 :(得分:0)
试试这段代码可以帮到你
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
答案 2 :(得分:0)
每条记录应包含联系人的一部分数据(例如,每个电话号码或地址是一个单独的行),每一行都有一个与之关联的mimetype,用于确定每列中存储的数据。所以对于一个地址,&#34; data1&#34;列保存街道数据,data4可能保持状态。