2017-04-18 97 views
-3

如何在從通訊錄中獲取聯繫人號碼時刪除Android中的重複號碼條目?如何從沒有數據庫的所有類型的聯繫人中刪除重複的聯繫人號碼?

例如: 在接觸書 一個名稱的多個聯繫人.. EX型家用9428060123,
型工作9428060123,我想從所有 型機器人抓取唯一聯繫號碼有問題?我用下面的代碼獲取
信息:

private void getContactsDetails() { 
showLoader(); 
      String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" 
        + ("1") + "'"; 
      Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    null, null/*selection + " AND " + 
    ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"*/, null, "UPPER(" 
    + ContactsContract.Contacts.DISPLAY_NAME + ") ASC"); 
      ContactString = new ArrayList<>(); 

      if (phones != null) { 

       if (phones.getCount() > 0) { 
        tvNoContact.setVisibility(View.GONE); 

        while (phones.moveToNext()) { 
         String Name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
         String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        HashSet<String> mobileNoSet = new HashSet<String>(); 
       if (!mobileNoSet.contains(number)){ 
         String s = number.replaceAll("\\W", ""); 
         String lastTenCharContact = null; 
         if (s != null && s.length() > 10) { 
          lastTenCharContact = s.substring(s.length() - 10); 
         } else { 
          lastTenCharContact = s; 
         } 
         // String substring = s.substring(Math.max(s.length() - 10, 0)); 
         Log.d(TAG, "getContactsDetails: " + lastTenCharContact); 
         String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 
         ContactString.add(lastTenCharContact); 

         DeviceContact contactModel = new DeviceContact(Name.toUpperCase(), lastTenCharContact, image_uri); 

         contactModelsList.add(contactModel); 
         mobileNoSet.add(number); 

         Log.d(TAG, "Name : " + Name + ", Number : " + number + ", Photo : " + image_uri);} 
        } 
        hideLoader(); 
        AllContactsyncapiCall(); 

       } else { 
        tvNoContact.setVisibility(View.VISIBLE); 
       } 
      } } 

回答

0

用於刪除重複的號碼你的循環之前只是讓HashSet的:

HashSet<String> mobileNoSet = new HashSet<String>(); 

現在,當你把接觸模型使用下面的代碼:

if (!mobileNoSet.contains(number)){ 

        String s = number.replaceAll("\\W", ""); 
        String lastTenCharContact = null; 
        if (s != null && s.length() > 10) { 
         lastTenCharContact = s.substring(s.length() - 10); 
        } else { 
         lastTenCharContact = s; 
        } 
        // String substring = s.substring(Math.max(s.length() - 10, 0)); 
        Log.d(TAG, "getContactsDetails: " + lastTenCharContact); 
        String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 
        ContactString.add(lastTenCharContact); 

        DeviceContact contactModel = new DeviceContact(Name.toUpperCase(), lastTenCharContact, image_uri); 
        contactModelsList.add(contactModel); 
        mobileNoSet.add(number); 
     } 
+0

我在我的應用程序中使用它,它工作完美。 mobileNoSet hashSet存儲號碼,如果從光標設置contais編號,則不允許將其添加到DeviceContact contactModel中。發佈您的新代碼 –

+0

因爲您在每個循環中都創建了新的Hashset,所以您將獲得重複條目。現在移動這一行HashSet mobileNoSet = new HashSet ();在tvNoContact.setVisibility(View.GONE)之後; –

+0

thanx ..它的工作,但是當我的設備與whatsapp或任何其他帳戶同步比我得到同樣的問題。 –

0

這是新的代碼,即時獲取重複的號碼: -

private void getContactsDetails() { 
    // showLoader(); 
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null/*selection + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"*/, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC"); 
    ContactString = new ArrayList<>(); 
    nameString = new ArrayList<>(); 

    if (phones != null) { 

     if (phones.getCount() > 0) { 
      tvNoContact.setVisibility(View.GONE); 
      HashSet<String> mobileNoSet = new HashSet<String>(); 
      while (phones.moveToNext()) { 
       String Name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

       if (!mobileNoSet.contains(number)) { 
        // String s = number.replaceAll("\\W", ""); 

        String s = number.replaceAll("[^[+]\\d]", ""); 

        String lastTenCharContact = null; 
        lastTenCharContact = s; 
       /*if (s != null && s.length() > 10) { 
        lastTenCharContact = s.substring(s.length() - 10); 
       } else { 
        lastTenCharContact = s; 
       }*/ 
        // String substring = s.substring(Math.max(s.length() - 10, 0)); 
        Log.d(TAG, "getContactsDetails: " + lastTenCharContact); 
        String image_uri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 
        ContactString.add(lastTenCharContact); 
        // nameString.add(Name); 
        contactModel = new DeviceContact(Name.toUpperCase(), lastTenCharContact, image_uri); 
        contactModelsList.add(contactModel); 
        mobileNoSet.add(number); 
        Log.d(TAG, "Name : " + Name + ", Number : " + number + ", Photo : " + image_uri); 
       } 
      } 
      //  hideLoader(); 
      AllContactsyncapiCall(); 

     } else { 
      tvNoContact.setVisibility(View.VISIBLE); 
     } 
    } 
} 
+0

我想保存contactModel = new DeviceContact(Name.toUpperCase(),lastTenCharContact,image_uri)中不同的聯繫人號碼; contactModelsList.add(contactModel); –

+0

你的代碼看起來不錯,如果在hashset檢查方法中打印的是相同的數字,你有沒有檢查日誌? –

+0

嘗試在if(!mobileNoSet。)之後立即放置mobileNoset.add(number)。包含(數字))條件 –

相關問題