重复联系人不显示在列表视图中

duplicate contacts not to show in listiview

我正在制作一个在 Listview 中显示联系人号码的应用程序,但它提供了重复的联系人条目,我只想单独显示一个联系人

 private void getContactList() {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {
            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));

            if (cur.getInt(cur.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                    ContactsPlacer obj = new ContactsPlacer();
                    obj.setContactname(name);
                    obj.setContactnumber(phoneNo);
                    names.add(obj);

                }
                pCur.close();
            }
        }
    }
    if(cur!=null){
        cur.close();
    }




}

帮助我解决列表视图中不显示重复联系人的问题

试试这个

if(email!=null){
    if(!names.contains(name)){
        names.add(name);
    }
}

这里像这样使用arraylist的contain方法

public ArrayList<String> getNameEmailDetails(){
    ArrayList<String> names = new ArrayList<String>();
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor cur1 = cr.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                    new String[]{id}, null);
            while (cur1.moveToNext()) {
                //to get the contact names
                String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                Log.e(TAG+"Name :", name);
                String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e(TAG+"Email", email);
                if(email!=null){
                   if(!names.contains(name))
                    names.add(name);
                }
            }
            cur1.close();
        }
    }
    return names;
}
boolean checkexsistance(ContactsPlacer obj)
{
boolean test= true;
for(int i = 0 ; i<names.size();i++)
{
if(names.get(i).getContactname().equals(obj.getContactname()))
{
    test = false;
    break;

}

}


return test;
}



private void getContactList() {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {
            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));

            if (cur.getInt(cur.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));


                    ContactsPlacer obj = new ContactsPlacer();
                    obj.setContactname(name);
                    obj.setContactnumber(phoneNo);
                    if(checkexsistance(obj))
                       names.add(obj);

                }
                pCur.close();
            }
        }
    }
    if(cur!=null){
        cur.close();
    }




}

几天前我遇到了同样的问题,并使用这段代码解决了同样的问题, 请试一试,如果需要进一步的帮助,请告诉我。

不要忘记征求许可并在清单中提及 "android.permission.READ_CONTACTS"

ContentResolver cr = getActivity().getContentResolver();
        String[] PROJECTION = new String[]{ContactsContract.RawContacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.RawContacts.VERSION};
        String order = "CASE WHEN "
                + ContactsContract.Contacts.DISPLAY_NAME
                + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
                + ContactsContract.Contacts.DISPLAY_NAME
                + ", "
                + ContactsContract.CommonDataKinds.Phone.DATA
                + " COLLATE NOCASE";
        String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, filter, null, order);

        for (int i = 0; i < cur.getColumnCount(); i++) {
            Timber.e("column " + i + "=" + cur.getColumnName(i));
        }

        if (cur.moveToFirst()) {
            do {
                String name = cur.getString(1);
                String number = cur.getString(4);

                number = Utils.removeExtraCharFromString(number);

                if (number.length() > 8) {
                    mContactNumbers = mContactNumbers + number + ",";
                    PhoneContacts phoneContacts = new PhoneContacts();
                    try {
                        phoneContacts.setName(URLEncoder.encode(name, "UTF-8"));
                        phoneContacts.setNumber(number);
                        phoneContacts.setNameInitial(String.valueOf(phoneContacts.getName().charAt(0)));

                        listOfContactsRaw.add(phoneContacts);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            } while (cur.moveToNext());
        }
        cur.close();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Set set = new TreeSet(new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {

                        PhoneContacts phoneContacts1 = (PhoneContacts) o1;
                        PhoneContacts phoneContacts2 = (PhoneContacts) o2;
                        if (phoneContacts1.getNumber().equalsIgnoreCase(phoneContacts2.getNumber())) {
                            return 0;
                        }
                        return 1;
                    }
                });
                set.addAll(listOfContactsRaw);

                System.out.println("\n***** After removing duplicates *******\n");

                listOfContacts = new ArrayList(set);

                // Set listOfContacts with your recycler view
            }
        }, 200);