2017-04-18 99 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)。包含(数字))条件 –

相关问题