从基于自定义类的列表中删除重复元素(哈希集解决方案不起作用)?

时间:2015-03-17 16:55:58

标签: android list duplicates

我有一个自定义列表,其中包含名称和电话号码的类对象。现在的问题是复制太多了。我需要根据电话号码删除所有重复项。

当我尝试这种方法时:

     list = getContacts();
    list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list));
    listView.setAdapter(new ContactsAdapter(this, list));  

ModelContact.java

        public class ModelContact {
            private String name;
            private String ContactNumber;
            private Bitmap imageUri;
            private String _id;
            public ModelContact() {
            }

            public Bitmap getImageUri() {
                return imageUri;
            }

            public void setImageUri(Bitmap imageUri) {
                this.imageUri = imageUri;
            }

            public String get_id() {
                return _id;
            }

            public void set_id(String _id) {
                this._id = _id;
            }



            public String getContactNumber() {
                return ContactNumber;
            }

            public void setContactNumber(String contactNumber) {
                ContactNumber = contactNumber;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
         @Override
     public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    if (obj instanceof ModelContact) {
        ModelContact temp = (ModelContact) obj;
        if (this.ContactNumber == temp.ContactNumber)
            return true;
    }
    return false;

}

 @Override
  public int hashCode() {
    // TODO Auto-generated method stub

    return (this.ContactNumber.hashCode());
}


        }

ContactsList:

  public class ContactsList extends ActionBarActivity {
        private ListView listView;
        ProgressBar progressBar;
        List<ModelContact> list;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_contacts);
            listView = (ListView) findViewById(R.id.contacts_list);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            list = new ArrayList<>();
            list = getContacts();
            list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list));
            listView.setAdapter(new ContactsAdapter(this, list));
        }

                private List<ModelContact> getContacts() {

                    ContentResolver cr = getContentResolver();
                    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                            null, null, null, null);
                    List<ModelContact> modelContacts = new ArrayList<>();
                    if (cur.getCount() > 0) {
                        ModelContact obj = new ModelContact();
                        while (cur.moveToNext()) {
                            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            if (Integer.parseInt(cur.getString(
                                    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));
                                    Bitmap bmp = retrieveContactPhoto(id);
                                    if (bmp == null) {
                                        System.out.println(name + "is \t null");
                                    } else {
                                        Toast.makeText(this, "not null \t" + phoneNo, Toast.LENGTH_SHORT).show();
                                    }
                                    String imagecon = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    obj.set_id(id);
                                    obj.setName(name);
                                    obj.setContactNumber(phoneNo);
                                    obj.setImageUri(bmp);
                                    modelContacts.add(obj);
                                    System.out.println("Name: " + name + ", Phone No: " + phoneNo);
                                    //Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                                }
                                pCur.close();
                            }
                        }
                    }
                    System.out.print("Size:::" + modelContacts.size());

                    return modelContacts;
                }

        private Bitmap retrieveContactPhoto(String contactID) {

            Bitmap photo = null;

            try {
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));

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

                if (inputStream != null)
                    inputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return null;
        }


    }

1 个答案:

答案 0 :(得分:0)

首先,您需要在ModelContact类中覆盖equals和hashcode方法。您需要在覆盖equals和hashcode方法时注意电话号码。

然后您可以使用下面的代码删除重复的元素

list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list))