Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从自定义基于类的列表中删除重复元素(哈希集解决方案不起作用)?_Android_List_Duplicates - Fatal编程技术网

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

Android 从自定义基于类的列表中删除重复元素(哈希集解决方案不起作用)?,android,list,duplicates,Android,List,Duplicates,我有一个自定义列表,其中包含具有名称和电话号码的类对象。。现在的问题是复制品太多了。我需要根据电话号码删除所有副本 当我尝试这种方法时: list = getContacts(); list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list)); listView.setAdapter(new ContactsAdapter(this, list)); 联系

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

当我尝试这种方法时:

     list = getContacts();
    list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list));
    listView.setAdapter(new ContactsAdapter(this, list));  
联系人列表:

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


    }

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

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

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

我仍然得到相同的结果,即列表中的1项。查看更新的代码。即使您的代码也应该可以工作,您只需更新map.putInteger.parseIntays.get\u phoneNo,ays;有关更多信息,请参阅此链接。问题是no.的格式不是整数,而是字符串,有时有特殊字符+或有时有空格。如何实现它?
list = new ArrayList<ModelContact>(new LinkedHashSet<ModelContact>(list))