0

I am able to fetch other information (Display name,organisation,phone no and email_id) of a contact, but not able to fetch birthday and anniversary of that contact.

Here is the code i am using for birthday. It does fetch the data, but gives me wrong data, i.e repeats the same data for all the contacts.

private String getBDate(String id) {
    String bday = null;
    ContentResolver cr = getContentResolver();
    Uri uri = ContactsContract.Data.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE };
    String where = ContactsContract.Data.MIMETYPE + "= ? AND "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
    String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
    String sortOrder = null;
    Cursor cur = cr.query(uri, projection, where, selectionArgs, sortOrder);
    while (cur.moveToNext()) {
        bday = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
        Log.v("Birthday", bday);
    }
    cur.close();
    return bday;
}

Same is the case with anniversary, here is the code for it. In some case anniversary is not added but it still shows the data from other contact.

private String getAnnv(String id) {
    String annv = null;
    ContentResolver cr = getContentResolver();
    Uri uri = ContactsContract.Data.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE };
    String where = ContactsContract.Data.MIMETYPE + "= ? AND "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY;
    String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
    // String sortOrder = null;
    Cursor cur = cr.query(uri, projection, where, selectionArgs, null);
    while (cur.moveToNext()) {
        annv = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
        Log.v("Anniversary", annv);
    }
    cur.close();
    return annv;
}
Beenal
  • 317
  • 1
  • 3
  • 14
  • I think it will be very useful to you http://stackoverflow.com/questions/2482631/how-to-get-contacts-in-order-of-their-upcoming-birthdays – Ramkumar Jul 30 '12 at 13:23
  • Actually in some question i didn't find the solution and so have not accepted that answer. – Beenal Aug 01 '12 at 09:35
  • I need to talk with you @Beenal . I need same birthdate and anniversary date from contact list. you got solution for this?? Please reply as soon as possible. if you got solution then please give me code soon. Its urgent please help me. – akhil batlawala Jul 23 '16 at 11:51

1 Answers1

0

you are not using String id perameter in where condition so please check again.

E,g private String getAnnv(String id) function has input for ID but that seems to be not used withing function so please put that ID in condition check and this should work.

e.g

ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=  " +  ID
AND ContactsContract.Data.MIMETYPE + "= ? AND "
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45