如何从没有数据库的所有类型的联系人中删除重复的联系号码?

时间:2017-04-18 12:21:28

标签: android contacts android-contacts

如何从联系人簿中提取联系人号码时删除Android中的重复号码条目?

例如: 在联系簿中     一个名称多个联系人.. ex-type-home-9428060123,
    type-work-9428060123,我想从所有人那里获取唯一的联系电话     类型,android有问题吗?我使用下面的代码来获取
    信息:

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

2 个答案:

答案 0 :(得分: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);
        }

答案 1 :(得分: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);
        }
    }
}