gpt4 book ai didi

java - 通过 PHONE.CONTENT_URI 检索联系人返回重复行

转载 作者:行者123 更新时间:2023-11-29 06:57:11 26 4
gpt4 key购买 nike

我正在尝试按帐户类型从我的原始联系人中检索电话号码。使用下面的代码段,

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 次,每个号码显示两次。我怎样才能最好地实现它以及我做错了什么?

最佳答案

您可以尝试使用 REMOVE_DUPLICATE_ENTRIES URI 上的参数:

Uri uri = Phone.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "1")
.build();

但是,如果您有两个具有相同号码的原始联系人,我认为您无法消除重复项,因为上述查询参数仅使 Android 应用 GROUP BY (RawContacts._ID, DATA1) 查询。

关于java - 通过 PHONE.CONTENT_URI 检索联系人返回重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32271158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com