How to get contacts in order of they upcoming birthdays?

Posted by Pentium10 on Stack Overflow See other posts from Stack Overflow or by Pentium10
Published on 2010-03-20T10:23:07Z Indexed on 2010/03/20 10:31 UTC
Read the original article Hit count: 496

I have code to read contacts details and to read birthday. But how do I get a list of contacts in order of they upcoming birthday?

For a single contact identified by id I get details and birthday like this.

Cursor c = null;
  try {
   Uri uri = ContentUris.withAppendedId(
     ContactsContract.Contacts.CONTENT_URI, id);
   c = ctx.getContentResolver().query(uri, null, null, null, null);
   if (c != null) {
    if (c.moveToFirst()) {
     DatabaseUtils.cursorRowToContentValues(c, data);
    }

   }
   c.close();

   // read birthday
   c = ctx.getContentResolver()
     .query(
       Data.CONTENT_URI,
       new String[] { Event.DATA },
       Data.CONTACT_ID + "=" + id + " AND "
         + Data.MIMETYPE + "= '"
         + Event.CONTENT_ITEM_TYPE + "' AND "
         + Event.TYPE + "=" + Event.TYPE_BIRTHDAY,
       null, Data.DISPLAY_NAME);
   if (c != null) {
    try {
     if (c.moveToFirst()) {
      this.setBirthday(c.getString(0));
     }
    } finally {
     c.close();
    }
   }

   return super.load(id);
  } catch (Exception e) {
   Log.v(TAG(), e.getMessage(), e);
   e.printStackTrace();
   return false;
  } finally {
   if (c != null)
    c.close();
  }

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk