Android Contact Phone Numbers fetching Duplication

心不动则不痛 提交于 2020-01-17 03:37:47

问题


I can fetch contacts and Phone Numbers of each contact but Phone Numbers are duplicate I think there is some option to show contacts linked with other apps like Viber etc so contact are retrieving duplicated

How can I avoid picking Duplicate Numbers of each contact ?


回答1:


Try this

 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
 startActivityForResult(intent, 3);
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 3) {
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            contactNumber = "";
            Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
            cursor.moveToFirst();
            String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            if (hasPhone.equals("1")) {
                Cursor phones = getContentResolver().query
                        (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    contactNumber = phones.getString(phones.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                    contactName=phones.getString(phones.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


                }
                phones.close();

And tell me if this solved your problem. :)




回答2:


I solved this issue by using hashmap. You made hashmap of device id to phone number. You add every number into hashmap before checking if hashmap doesnot contain that device id.

Get device id by Querying following URI.

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME};

Hashmap<String , Contact > = new Hashmap<String , Contact>();

if (contactHashMap.containsKey(id)) {
   // skip that contact.
}
else {
    //fill your contact object.
    // get device id of contact from mobile                     
       contactHashMap.put(id, contact);
}

In this way u can avoid duplicate. There is another way to query other table which gives number to device id. But this cost u two queries. Above hashmap method is efficient and worked well for me.



来源:https://stackoverflow.com/questions/40846337/android-contact-phone-numbers-fetching-duplication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!