2015-08-28 81 views
1

我試圖從我的原始聯繫人的帳戶類型檢索電話號碼。 使用下面的代碼片段,通過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次,顯示每個數字兩次。我如何最好地實現這一點,我做錯了什麼?

+0

這是一個可能的錯誤,但它不涉及您如何查詢數據。這更多的是與Google相關的問題。如果您嘗試打開庫存Lollipop Messenger應用程序,則會在「新消息」部分中看到重複的行。我還試圖通過 – Matteo

回答

6

您可以嘗試使用中的URI,REMOVE_DUPLICATE_ENTRIES參數:

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

但是,如果你有相同數量的兩個原始接觸中,我不認爲你可以消除重複,因爲上面的查詢參數僅使Android向查詢應用GROUP BY (RawContacts._ID, DATA1)

+0

解決這個問題ContactsContract.REMOVE_DUPLICATE_ENTRIES被添加到API級別21中。 – Sahil

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); 

    }