【问题标题】:how to remove duplicate contacts from arraylist如何从arraylist中删除重复的联系人
【发布时间】:2014-04-09 10:22:16
【问题描述】:

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

我该怎么做?

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

【问题讨论】:

  • 你解决了这个问题吗?

标签: android list


【解决方案1】:
  • 如果您想消除重复项,请考虑改用HashSet

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

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

【讨论】:

  • 我使用了一个散列集,但它给了我未处理的数组,并且其中也存在重复值..,并且通过使用上面的代码,什么也没有发生
  • @Himani override equals() 方法来区分你的 ProfileBean 对象等价性...
  • @Gopal Rao 怎么办..??
  • @Himani 请参阅documentation 关于覆盖equals() 方法的正确方法...更多的是它不是android 概念,它是一个简单的普通java 概念...
  • @Himani a Set 不是 Array,由于其性质,您不会看到它已排序,但它只允许一个 具有相同名称的项目,您不可能在这里有重复项。如果是这样,你做错了什么。
【解决方案2】:

在添加到列表之前,在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));
}

【讨论】:

  • @Sanawaj 相同的结果,现在列表中没有显示任何内容
  • @Sanawaj 你告诉我的事情,在这种情况下,编译器不会只进入 for 循环,它将如何进一步进行
  • @Sanawaj 怎么工作,又不会进入for循环。
  • @Sanawaj in list values 是通过 profilebean 添加的。,意味着通过这个 list.add(new ProfileBean(name, phoneNumber));在此之前它什么都没有
  • 在phone.close()之前的mainactivity中
【解决方案3】:

以下函数可用于从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;
}

【讨论】:

    【解决方案4】:

    使用下面的代码 list=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;
    }
    

    【讨论】:

      【解决方案5】:

      尝试获取 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();
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-18
        • 2017-07-18
        • 1970-01-01
        • 2021-02-08
        • 2014-09-14
        • 2023-03-23
        • 2020-03-06
        • 1970-01-01
        相关资源
        最近更新 更多