如何从arraylist中删除重复的联系人

时间:2014-04-09 10:22:17

标签: android list

我创建了一个应用程序,其中我从设备获取联系人。 但我想从结果中删除重复的联系人。

我怎么能这样做?

MainActivity

public class MainActivity extends Activity implements OnItemClickListener {

EditText searchText;

ArrayList<String> phno0 = new ArrayList<String>();
List<String> arrayListNames;
public List<ProfileBean> list;
public SearchableAdapter adapter;
//ProfileBean bean;
String[] cellArray = null;
String contacts;
ListView lv;
String phoneNumber, name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();


    lv = (ListView) findViewById(R.id.listview);
    list = new ArrayList<ProfileBean>();
    getAllCallLogs(this.getContentResolver());
    adapter = new SearchableAdapter(getApplication(), list);
    lv.setAdapter(adapter);
    lv.setItemsCanFocus(false);
    lv.setOnItemClickListener(this);
    lv.setTextFilterEnabled(true);


}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

}

public void getAllCallLogs(ContentResolver cr) {

    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                    + " ASC");
    while (phones.moveToNext()) {
        phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        list.add(new ProfileBean(name, phoneNumber));

    }
    phones.close();
}
}

5 个答案:

答案 0 :(得分:5)

  • 如果您想摆脱重复,请考虑使用HashSet代替。

  • 如果您不想使用它,只需在添加联系人是否已经存在之前进行检查。

    if (!myList.contains(newContact))
      myList.add(newContact);
    

答案 1 :(得分:1)

在添加到列表之前,在list.add(new ProfileBean(name, phoneNumber));的位置添加以下检查:

int flag = 0
if(list.size() == 0){
list.add(new ProfileBean(name, phoneNumber));
}
    for(int i=0;i<list.size();i++){

    if(!list.get(i).getProfileName().trim().equals(name)){
    flag = 1;

    }else{
     flag =0;
     break;

}

    }
if(flag == 1){
list.add(new ProfileBean(name, phoneNumber));
}

答案 2 :(得分:0)

以下函数可用于从String ArrayList中删除重复项根据您的要求进行更改

public ArrayList<String> listWithoutDuplicates(ArrayList<String> duplicateList) {

    // Converting ArrayList to HashSet to remove duplicates
    LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);

    // Creating Arraylist without duplicate values
    ArrayList<String> listWithoutDuplicates = new ArrayList<String>(listToSet);

    return listWithoutDuplicates;
}

答案 3 :(得分:0)

使用下面的代码列表= removeDuplicates(list);

public List<ProfileBean> removeDuplicates(List<ProfileBean> list) {
    // Set set1 = new LinkedHashSet(list);
    Set set = new TreeSet(new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            if (((ProfileBean) o1).getName().equalsIgnoreCase(((ProfileBean) o2).getName()) &&
                    ((ProfileBean)o1).getPhoneNumber().equalsIgnoreCase(((ProfileBean)o2).getPhoneNumber())) {
                return 0;
            }
            return 1;
        }
    });
    set.addAll(list);

    final List newList = new ArrayList(set);
    return newList;
}

答案 4 :(得分:0)

尝试获取ContactsContract.Contacts.NAME_RAW_CONTACT_ID)的唯一ID,并将其用于更新联系人,以比较您的原始ID与下面的联系人是否相同

    private void getAllContactsBackground() {

    ContentResolver contentResolver = getActivity().getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));

                Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
                Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);


                Bitmap photo = null;
                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream);
                }


                while (cursorInfo.moveToNext()) {
                    ContactsModel info = new ContactsModel();
                    info.contacts_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    info.contacts_raw_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
                    info.contacts_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    info.contacts_mobile = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceFirst("[^0-9]" + "91", "");
                    info.contacts_photo = photo;
                    info.contacts_photoURI = String.valueOf(pURI);


                    Cursor emailCur = contentResolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (emailCur.moveToNext()) {

                        info.contacts_email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        Log.e("email==>", "" + info.contacts_email);

                    }
                    emailCur.close();


                    int flag = 0;
                    if (arrayListAllContacts.size() == 0) {
                        arrayListAllContacts.add(info);
                    }
                    for (int i = 0; i < arrayListAllContacts.size(); i++) {

                        if (!arrayListAllContacts.get(i).getContacts_raw_id().trim().equals(info.contacts_raw_id)) {
                            flag = 1;

                        } else {
                            flag = 0;
                            break;
                        }

                    }
                    if (flag == 1) {
                        arrayListAllContacts.add(info);
                    }


                }
                cursorInfo.close();

            }
        }
        cursor.close();

    }
}