Android Contact Phone Numbers Fetching Duplication
I can fetch contacts and Phone Numbers of each contact but Phone Numbers are duplicate I think there is some option to show contacts linked with other apps like Viber etc so contac
Solution 1:
Try this
Intentintent=newIntent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 3);
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3) {
if (resultCode == RESULT_OK) {
UricontactData= data.getData();
contactNumber = "";
Cursorcursor= getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
StringhasPhone= cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
StringcontactId= cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
if (hasPhone.equals("1")) {
Cursorphones= getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
contactNumber = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
contactName=phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
phones.close();
And tell me if this solved your problem. :)
Solution 2:
I solved this issue by using hashmap. You made hashmap of device id to phone number. You add every number into hashmap before checking if hashmap doesnot contain that device id.
Get device id by Querying following URI.
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = newString[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME};
Hashmap<String , Contact > = newHashmap<String , Contact>();
if (contactHashMap.containsKey(id)) {
// skip that contact.
}
else {
//fill your contact object.// get device id of contact from mobile
contactHashMap.put(id, contact);
}
In this way u can avoid duplicate. There is another way to query other table which gives number to device id. But this cost u two queries. Above hashmap method is efficient and worked well for me.
Post a Comment for "Android Contact Phone Numbers Fetching Duplication"