我試圖從我的原始聯繫人的帳戶類型檢索電話號碼。 使用下面的代碼片段,通過PHONE.CONTENT_URI檢索聯繫人返回重複行
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(字符串的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接觸,我的聯繫人列表,但同時匹配這樣的查詢循環運行6次,顯示每個數字兩次。我如何最好地實現這一點,我做錯了什麼?
這是一個可能的錯誤,但它不涉及您如何查詢數據。這更多的是與Google相關的問題。如果您嘗試打開庫存Lollipop Messenger應用程序,則會在「新消息」部分中看到重複的行。我還試圖通過 – Matteo