I am having an issue related to contacts. I got the phone contacts and stored them in my list object. Here's the code for it
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_ID,
ContactsContract.Data.DATA1
};
Cursor phones = getContentResolver().query(
uri, projection, ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''", null, null);
if (phones.moveToFirst()) {
do {
long ID = phones.getLong(phones.getColumnIndex(projection[0]));
String DisplayName = phones.getString(phones.getColumnIndex(projection[1]));
String photoID = phones.getString(phones.getColumnIndex(projection[2]));
String Key = phones.getString(phones.getColumnIndex(projection[3]));
String photoURI = "null";
if(Key != null && Key.toString().trim().length() > 0 && (Key.startsWith("0") || Key.startsWith("+"))){
if (photoID != null) {
photoURI=String.valueOf(ID);;
//Console.WriteLine("*************************************> id="+ID+" uri="+photoURI.ToString());
}
ContactBean contactModel=new ContactBean(DisplayName,Key,photoID);
list.add(contactModel);
} else {
// No number!!
}
} while (phones.moveToNext());
}
I am getting all the contacts and email contacts are removed as per my requirements. My issue is i am getting all the contacts including duplicate ones. If i have a contact saved 3 times with same name and number it is getting all the three contacts. I do not want this. Is there any way to avoid this. Anything in getContactResolver query or I have to remove duplicates for my list. Any solutions or suggestions?