I am trying to read the birthdays of all my contacts. My code works prefectly fine with most of the contacts, but I get a little problem with a few contacts as they appear twice in my cursor. Regarding to my researches this does always happen when a contact for example is both stored in the device manually and imported from skype to the contacts. I haven't tested it with other imported contacts (like facebook etc.), but I assume I will run into the same problem there. Here is my code so far:
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.START_DATE };
String selection = ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "= ? AND "+
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
Integer.toString(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY),
"1"};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null);
while (c.moveToNext()) {
String id = c.getString(0);
String name = c.getString(1);
String bday = c.getString(2);
Log.d(TAG, "id: " + id + ", name: " + name + ", bday: " + bday);
}
When I run this code my contact imported from Skype will show up twice in the logcat:
id: 251, name: Homer Simpson, bday: 1956-05-12
id: 502, name: Homer Simpson, bday: 12.05.1956
As you can see the entry has different IDs so I cannot solve my problem via the _ID attribute (Someone suggested that here: Android Applicaiton - How to get birthday of a contact). I also see Homer Simpson only once in my contact list. The perfect solution would be to only get the birthday from skype, when there is no birthday for that contact stored in my device manually. I started looking around for a solution and found a lot suggestions using the LOOKUP_KEY, but I struggled in implementing this and am not sure if that's the correct way to solve my problem.
Does anyone have any ideas how to fix this? Every approach will be appreciated! Thank you !