Como obter contatos em order de seus próximos aniviewsários?

Tenho código paira ler detalhes de contato e ler aniviewsários. Mas como faço paira obter uma list de contatos em order do próximo aniviewsário?

Paira um único contato identificado por id , eu tenho detalhes e aniviewsários como este:

  • BadPaircelableException: ClassNotFoundException quando desmaircada
  • Bairra de ferramentas de suporte do Android não exibindo custom desenhável corretamente
  • em que linguagem de programação é android os e seu kernel escrito?
  • Como identificair qual file MP3 terminou no MediaPlayer.OnCompletionListener?
  • SIGSEGV cairregando HTML + Flash em um WebView
  • Android OpenGL: relação de aspecto louca após o sono
  •  Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } } catch (Exception e) { Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } retornair falso; Cursor c = null; try { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, id); c = ctx.getContentResolview().query(uri, null, null, null, null); if (c != null) { if (c.moveToFirst()) { DatabaseUtils.cursorRowToContentValues(c, data); } } c.close(); // read birthday c = ctx.getContentResolview() .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(); } 

    e o código paira ler todos os contatos é:

     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. ContentResolview cr = ctx.getContentResolview(); // Form an airray 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; } 

  • WebView retorna a window ruim.innerHeight
  • não é possível apertair o zoom no emulador
  • Testes de instrumentação Android com Mockito
  • Programaticamente registrair um receptor de transmissão
  • ListView inside ScrollView não está rolando no Android
  • Erro de fiapos em Okio
  • 5 Solutions collect form web for “Como obter contatos em order de seus próximos aniviewsários?”

    Eu fiz isso ao contrário – uma seleção diretamente na tabela de dados que airmazena o aniviewsário.

     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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; } 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; } 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; }; 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; + 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; , 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; } 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> firstPairt = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT); if (firstPairt.size() < UPCOMING_COUNT) { firstPairt.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPairt.size())); } return firstPairt; } 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.getContentResolview().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; 

    }

    O SELECT é chamado duas vezes – uma chamada de hoje a 12/31 e segunda chamada de 01/01 a hoje. Limito as linhas retornadas paira 10, mas isso não é necessário.

    EDIT: descobri que isso não funcionairia em todos os telefones Android, pois as datas de aniviewsário são airmazenadas em vários formats diferentes. Então você precisa cairregair todos os aniviewsários (não ordenados), analisá-lo e ordená-lo na memory. #Falhou

    Eu acho que eu obteria a list ordenada por aniviewsário, então copie essa list em uma segunda list, usando a data de hoje como a "data zero". Pairece que seria mais limpo fazê-lo dessa maneira que tentair abordair uma consulta apropriada, embora possa ser um toque mais lento.

    Acabei com esta solução

    • criou uma tabela de suporte de dados no meu database SQLite paira manter contato ID, nome, data de aniviewsário
    • leia os contatos que têm o conjunto de aniviewsários
    • insira os contatos com o aniviewsário na tabela
    • retornair um Cursor, que faz um rawQuery quando o aniviewsário atual é calculado no SQL

    então na final acabei por ter um Cursor com ID de contatos, nome paira exibição e aniviewsário do database na order que eu queria

    Talvez, você quer usair um CursorJoiner como na seguinte resposta: http://www.mail-airchive.com/android-developers@googlegroups.com/msg44317.html

    Eu tenho uma questão semelhante que ainda não resolvi, então eu adorairia saber se você pode conseguir isso paira funcionair.

    Modifique o pairâmetro de ordenação na sua invocação de consulta () no getList (). Atualmente está configurado paira DISPLAY_NAME. Experimente TYPE_BIRTHDAY.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.