How to get contacts in the order of their upcoming birthdays?

I have code for reading contact details and reading birthdays. But how do I get a list of contacts in the order of their upcoming birthday?

For one contact identified by id , I get the details and birthday as follows:

 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(); } 

and code for reading all contacts:

 public Cursor getList() { // Get the base URI for the People table in the Contacts content // provider. Uri contacts = ContactsContract.Contacts.CONTENT_URI; // Make the query. ContentResolver cr = ctx.getContentResolver(); // Form an array specifying which columns to return. String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; Cursor managedCursor = cr.query(contacts, projection, null, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); return managedCursor; } 
+7
java android android-contentresolver sqlite
source share
5 answers

I did it the other way around: choosing directly in the data table in which the birthday is stored.

 private static final int UPCOMING_COUNT = 10; public static List<BContact> upcomingBirthday(Context ctx) { String today = new SimpleDateFormat("MM-dd").format(new Date()); List<BContact> firstPart = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPart.size() < UPCOMING_COUNT) { firstPart.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPart.size())); } return firstPart; } public static List<BContact> upcomingBirthday(Context ctx, String fromDate, String toDate, int rows) { Uri dataUri = ContactsContract.Data.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Event.CONTACT_ID, ContactsContract.CommonDataKinds.Event.START_DATE }; Cursor c = ctx.getContentResolver().query( dataUri, projection, ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) >= ?" + " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) <= ?" , new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, fromDate, toDate}, "substr("+ ContactsContract.CommonDataKinds.Event.START_DATE +",6)"); List<BContact> result = new ArrayList<BContact>(); int i=0; while (c.moveToNext() && i<rows) { result.add(new BContact(c.getString(0), c.getString(1), c.getString(2))); i++; } c.close(); return result; 

}

SELECT is called twice - one call from today to 12/31 and a second call from 01/01 to today. I limit the returned rows to 10, but this is optional.

EDIT: I found that this will not work on all Android phones, as birth dates are stored in different formats. So you have to load all birthdays (unordered), disassemble it and sort in memory. #Fail

+1
source share

I think we get a list sorted by birthday, and then copy this list to the second list, using today's date as a β€œzero date”. It seems that it would be easier to do this than to try to work out the corresponding request, although this may be slower.

+1
source share

I ended up with this solution

  • created a data storage table in my SQLite database to store the contact ID, name, date of birth
  • read happy birthday contacts
  • insert happy birthday contacts into the table
  • returns a cursor that executes rawQuery when the current birthday is calculated in SQL

so in the final, I got a cursor with the contact identifier, display name and birthday from the database in the order in which I wanted

+1
source share

Perhaps you want to use CursorJoiner, as in the following answer: http://www.mail-archive.com/android-developers@googlegroups.com/msg44317.html

I have a similar problem that I haven't solved yet, so I would like to know if you can make this work.

0
source share

Change the sort parameter in the query () call to getList (). Currently set to DISPLAY_NAME. Try TYPE_BIRTHDAY.

-one
source share

All Articles