Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to remove duplicate from list

KarthikKPN:

I am working in an Contact app where i have to list all the contacts from Contacts Database, but it contains duplicates.

For example, Contact number (98*******33, +9198********33) is listed as duplicates.

I have used Set, Checked whether my list contains the phone number before adding but none of this works !

ContactVO is the Pojo class and contactVoList is the list that i am adding contacts.


Cursor phones = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(" ", "");
imageUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("Name and Phone number = " + name + phoneNumber + imageUri);

if (contactVOList.size() == 0) {
contactVOList.add(new ContactVO(imageUri, name, phoneNumber, false));
} else {
if (!contactVOList.contains(phoneNumber)) {
contactVOList.add(new ContactVO(imageUri, name, phoneNumber, false));
}
}

System.out.println("List size before removing duplicates =" + contactVOList.size());
}

Set s = new HashSet();
s.addAll(contactVOList);
contactVOList = new ArrayList();
contactVOList.addAll(s);

My Pojo class


public class ContactVO {
private String ContactImage;
private String ContactName;
private String ContactNumber;
private int hashCode;
String id;
boolean clicked;

public ContactVO(String id) {
this.id = id;
}

public boolean isClicked() {
return clicked;
}

public void setClicked(boolean clicked) {
this.clicked = clicked;
}

public String getId() {


return id;
}

public void setId(String id) {
this.id = id;
}

public ContactVO(String ContactImage, String ContactName, String ContactNumber, Boolean clicked) {
this.ContactImage = ContactImage;
this.ContactName = ContactName;
this.ContactNumber = ContactNumber;
this.clicked = clicked;
}


public String getContactImage() {
return ContactImage;
}

public void setContactImage(String contactImage) {
this.ContactImage = ContactImage;
}

public String getContactName() {
return ContactName;
}

public void setContactName(String contactName) {
ContactName = contactName;
}

public String getContactNumber() {
return ContactNumber;
}

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

@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stubs
if (obj instanceof ContactVO) {
ContactVO temp = (ContactVO) obj;
System.out.println("this.getctno" + this.getContactNumber());
System.out.println("temp.getctno" + temp.getContactNumber());
if (this.getContactNumber() == temp.getContactNumber() && this.getContactName() == temp.getContactName() && (this.getContactNumber()).contains(temp.getContactNumber()))
return false;
}
return true;
}

@Override

public int hashCode() {
// TODO Auto-generated method stub
return (this.getContactNumber().hashCode() + this.getContactName().hashCode());
}
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to remove duplicate from list

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×