I'm getting same contact three or two times in my app this happening with some contacts not with every contacts. In my app everything is working as expected but when clicking on show contact from my it's shows three time same contact but in mobile phone contact stored only one time. I tried everything from my side but not able to solve this can any body please help me. Or is there any alternative way for same.
Here is my code:-
@Override
protected Integer doInBackground(Void... params) {
try {
db = new WhooshhDB(myContext);
this.list = new ArrayList<>();
ContentResolver cr = myContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(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));
ContactModel model = new ContactModel();
if (phoneNo.replaceAll("\\s", "").trim().length() > 7) {
model.name = name;
model.mobileNumber = phoneNo.replaceAll("\\s", "");
if (model.mobileNumber.contains("-")) {
model.mobileNumber = model.mobileNumber.replaceAll("-", "");
}
model.iconHexColor = AppConstant.getRandomSubscriptionHexColor(myContext);
if (!phoneNumber.equals(model.mobileNumber)) {
list.add(model);
}
}
Log.i("FetchContacts", "Name: " + name);
Log.i("FetchContacts", "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
return AppConstant.SUCCESS;
} catch (Exception ex) {
return null;
}
}
Get rid of
Change it to
You're printing those "FetchContacts" logs for per contact per phone, so if a contact has multiple phones stored for her you'll see it printed multiple times (even if it's the same phone number).
If you have an app like Whatsapp installed, then almost always you'll see all phone number duplicated for each contact causing those logs to be printed more then once per contact.
Also, that's a slow and painful way of getting contacts w/ phones. Instead you can simply query directly over Phones.CONTENT_URI and get all phones in the DB, and map them out into contacts by Contact-ID: