Android get phone contacts and remove duplicates

后端 未结 5 2030
天命终不由人
天命终不由人 2021-01-14 11:53

I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here\'s the code for it

  Uri uri = ContactsContract.Da         


        
5条回答
  •  死守一世寂寞
    2021-01-14 12:33

     String lastnumber = "0";
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, Constants.PROJECTION, null, null, null);
        if (cursor != null) {
            try {
                final int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String name, number;
                while (cursor.moveToNext()) {
                    name = cursor.getString(nameIndex);
                    number = cursor.getString(numberIndex).trim();
                    number = number.replaceAll("\\s", "");
                    if (number.equals(lastnumber)) {
    
                    } else {
                        lastnumber = number;
                        Contact contact = new Contact();
                        contact.name = name;
                        contact.phone = number;
                        mContactList.add(contact);
                        if (adapter != null)
                            adapter.notifyDataSetChanged();
                        System.out.println("ContactFragment.readContact ==>" + name);
                    }
                }
            } finally {
                cursor.close();
            }
        }
    

提交回复
热议问题