我正在尝试按帐户类型从原始联系人中检索电话号码。 使用下面的代码段,
String SELECTION =
ContactsContract.RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI,
null, SELECTION, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String type = cur.getString(cur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//String phone = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("number",name);
// hasPhoneNumber(cur.getString(cur.getColumnIndex(ContactsContract.RawContacts._ID)));
}
cur.close();
}
我可以检索绑定到帐户类型的所有联系人,但是,hasPhoneNumber(String contactId)返回一个空光标。
private boolean hasPhoneNumber(String id) {
Cursor pCur = this.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNo = phoneNo.replace(" ", "");
if (Integer.parseInt(pCur.getString(
pCur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Log.e("search", "found phone number");
pCur.close();
return true;
}
pCur.close();
}
return false;
}
但是,我决定用PHONE.CONTENT_URL执行我的查询:
String SELECTION =
ContactsContract.RawContacts.ACCOUNT_TYPE + "='" + Constants.ACCOUNT_TYPE + "'";
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, SELECTION, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String type = cur.getString(cur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phone = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("number",phone);
// hasPhoneNumber(cur.getString(cur.getColumnIndex(ContactsContract.RawContacts._ID)));
}
cur.close();
}
除了我的联系人列表中只有3个与此类查询匹配的联系人,但是while循环运行6次,每次显示两次以上时,这个工作正常。我怎样才能最好地实现这个以及我做错了什么?
答案 0 :(得分:6)
您可以尝试使用URI上的REMOVE_DUPLICATE_ENTRIES参数:
Uri uri = Phone.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "1")
.build();
但是,如果您有两个具有相同号码的原始联系人,我认为您不能删除重复项,因为上述查询参数仅使Android将GROUP BY (RawContacts._ID, DATA1)
应用于查询。
答案 1 :(得分:0)
以下是我在查询ContactsContract.CommonDataKinds.Phone.CONTENT_URI
时删除重复项的方法。我有ArrayList
持有所有联系人。请注意我使用布尔值addNewContact
做了什么。我检查如果我有另一个联系人使用相同的CONTACT_ID
,如果我没有,那么它将添加另一个联系人,但如果有,那么它将跳过添加该联系人。请参阅此处的完整代码段:
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
cur.moveToPosition(-1);
boolean addNewContact;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String thumb = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
String id = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (allContacts==null)
allContacts = new Vector<Contact>();
addNewContact = true;
for (Contact addedC:allContacts){
if (addedC.id.equals(id))
addNewContact = false;
}
if (addNewContact) {
Contact c = new Contact(name, id);
if (thumb != null)
c.setThumb(cr, thumb);
allContacts.add(c);
}
}
}
这是我的Contact
课程:
public class Contact {
String name;
Bitmap thumb;
String id;
public Contact(String name, String id){
this.name = name;
this.id = id;
}
public void setThumb(ContentResolver cr, String uri) {
try {
thumb = MediaStore.Images.Media.getBitmap(cr, Uri.parse(uri));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (thumb!=null)
thumb = getCroppedBitmap(thumb);
}