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.Da
You can try this :
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
String lastnumber = "0";
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String number = null;
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())
{
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("lastnumber ", lastnumber);
Log.e("number", number);
if (number.equals(lastnumber))
{
}
else
{
lastnumber = number;
Log.e("lastnumber ", lastnumber);
int type = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
switch (type)
{
case Phone.TYPE_HOME:
Log.e("Not Inserted", "Not inserted");
break;
case Phone.TYPE_MOBILE:
databaseHandler.insertContact(id, name, lastnumber, 0);
break;
case Phone.TYPE_WORK:
Log.e("Not Inserted", "Not inserted");
break;
}
}
}
pCur.close();
}
}
}
Here i have inserted data in sqlite database first and then Write select query with group by name.
Hope it helps
Use PhoneNumberUtils.compare(a, b) to filter out duplicated numbers
val contacts = ArrayList<MyContact>()
val uniqueMobilePhones = ArrayList<String>()
while (cursorPhones.moveToNext()) {
val displayName = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
val number = cursorPhones.getString(cursorPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val convertedNumber = convert(telman, number)
var duplicate = false
uniqueMobilePhones.forEach { addedNumber ->
if (PhoneNumberUtils.compare(addedNumber, number)) {
duplicate = true
}
}
if (!duplicate) {
uniqueMobilePhones.add(number)
contacts.add(MyContact(displayName, number, convertedNumber.replace(Regex("[ -+()]"), "")))
}
}
String lastnumber = "0";
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, Constants.PROJECTION, null, null, null);
if (cursor != null) {
try {
final int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name, number;
while (cursor.moveToNext()) {
name = cursor.getString(nameIndex);
number = cursor.getString(numberIndex).trim();
number = number.replaceAll("\\s", "");
if (number.equals(lastnumber)) {
} else {
lastnumber = number;
Contact contact = new Contact();
contact.name = name;
contact.phone = number;
mContactList.add(contact);
if (adapter != null)
adapter.notifyDataSetChanged();
System.out.println("ContactFragment.readContact ==>" + name);
}
}
} finally {
cursor.close();
}
}
Having multiple contacts using content provider/cursor loader is obvious since we are querying raw contacts list. My way of removing duplicate items is overriding hashcode and equals method. Below is my code which will avoid adding multiple contacts to the list.
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
A model class contains below fields. You can modify as you need.
private String name;
private String number;
private boolean isSelected;
Now override the hashcode and equals method in the model class.
@Override
public boolean equals(Object v) {
boolean retVal = false;
if (v instanceof SelectableContact){
SelectableContact ptr = (SelectableContact) v;
if(ptr != null) {
//We can add some regexpressions to ignore special characters or spaces but
// this consumes a lot of memory and slows down the contact loading.
if(!TextUtils.isEmpty(ptr.number) && !TextUtils.isEmpty(this.number) && ptr.number.equalsIgnoreCase(this.number)) {
retVal = true;
}//if
}
}
return retVal;
}
@Override
public int hashCode() {
int hash = 7;
hash = 17 * hash + (this.number != null ? this.number.hashCode() : 0);
return hash;
}
Now it is good to go. If the contents of the list items are same, it will state away reject while adding to the list. Look into below example.Let my model class be Contact.
public class Contact implements implements Parcelable {
}
Once you get the contacts from contentProvider or from ContactCursor loader, perform this action.
List<Contact> contactList = new ArraList<>;
Contact contact = new Contact();
if(!contactList.contains(contact)) {
//add contact to list.
}else {
//remove contact from list.
}
The hashcode and equals method will compare the contents of the list item before adding. If the same contents are present it will remove.
It is good to go.
For more information refer Why do I need to override the equals and hashCode methods in Java?
Here the code below is to find the duplicate number in your contact list and also the frequency that how many the number occur in your contact list
public ArrayList<ContactToDelete> findDuplicates(ArrayList<Contact> listContainingDuplicates) {
ArrayList<Contact> duplicatesOrganised = new ArrayList();
ArrayList<ContactToDelete> setToReturn = new ArrayList();
// Collections.sort(listContainingDuplicates);
Collections.sort(listContainingDuplicates, new Comparator<Contact>() {
public int compare(Contact obj1, Contact obj2) {
// ## Ascending order
return obj1.getPhoneNumber().compareToIgnoreCase(obj2.getPhoneNumber()); // To compare string values
// return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values
// ## Descending order
// return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values
// return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer values
}
});
int ii, size = listContainingDuplicates.size();
//Orders all the duplicates together along with the unique(non-duplicate)
for (ii = 0; ii < size; ii++) {
if (ii + 1 == size) {
duplicatesOrganised.add(listContainingDuplicates.get(ii));
Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
} else if (listContainingDuplicates.get(ii).getPhoneNumber().equals(listContainingDuplicates.get(ii + 1).getPhoneNumber())) {
duplicatesOrganised.add(listContainingDuplicates.get(ii));
Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
} else {
duplicatesOrganised.add(listContainingDuplicates.get(ii));
Log.i("DuplicateOrdered: ", listContainingDuplicates.get(ii).getPhoneNumber() + " " + listContainingDuplicates.get(ii).getName());
}
}
int firstcome = 0;
int start = 0;
boolean present = false;
boolean duplicatefond = false;
int startsetToReturn = 0;
if (!duplicatesOrganised.isEmpty() &&
duplicatesOrganised.size() > 1 &&
!duplicatesOrganised.get(0).getPhoneNumber().equals(duplicatesOrganised.get(1).getPhoneNumber())) {
start = 1;
}
for (int i = 0; i < duplicatesOrganised.size(); i++) {
String currentNumber = duplicatesOrganised.get(i).getPhoneNumber();
if(setToReturn.size()>0){
for (int j = 0; j <setToReturn.size() ; j++) {
if(setToReturn.get(j).getNumber().equals(currentNumber)){
present = true;
}
}
}
int flag = 0;
if(!present) {
for (int j = i+1; j < duplicatesOrganised.size(); j++) {
if (duplicatesOrganised.get(j).getPhoneNumber().equals(currentNumber)) {
duplicatefond = true;
if (flag == 0) {
flag++;
setToReturn.add(new ContactToDelete(duplicatesOrganised.get(i).getPhoneNumber(), duplicatesOrganised.get(i).getName(), flag));
} else
{
flag++;
setToReturn.get(startsetToReturn).setRepeatValue(flag);
}
}
}
if(duplicatefond) {
startsetToReturn++;
duplicatefond = false;
}
}
firstcome = 0;
present = false;
}
startsetToReturn =0;
Log.e("setToReturn", setToReturn.toString());
return setToReturn;
}
The two model class which I use one is for getting the full contact list and the other is to get the duplictae number
No 1 model Class
public class ContactToDelete {
private String number;
private String name;
private int repeatValue;
private String contactID;
public ContactToDelete(String number, String name, int repeatValue, String contactID) {
this.number = number;
this.name = name;
this.repeatValue = repeatValue;
this.contactID = contactID;
}
public ContactToDelete() {
}
public ContactToDelete(String number, String name, int repeatValue) {
this.number = number;
this.name = name;
this.repeatValue = repeatValue;
}
public String getContactID() {
return contactID;
}
public void setContactID(String contactID) {
this.contactID = contactID;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRepeatValue() {
return repeatValue;
}
public void setRepeatValue(int repeatValue) {
this.repeatValue = repeatValue;
}
}
No 2 Model Class
public class Contact {
public String name;
public String phoneNumber;
public Contact(String name, String phoneNumber, String contactID) {
this.name = name;
this.phoneNumber = phoneNumber;
this.contactID = contactID;
}
public String getContactID() {
return contactID;
}
public void setContactID(String contactID) {
this.contactID = contactID;
}
public String contactID;
public Contact() {
}
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}