2012-04-06 15 views
0

Ich versuche, Informationen zu einem Kontakt Geburtstag zu erhalten. Ich verwende Lookupkey, um meine Kontakte zu identifizieren (da es sicherer ist als nur contactId zu verwenden). Um i ein raw_contact_id muß in der Lage, die Veranstaltung in die Datenbank einfügen ... also ich versuche, diese ID zu erhalten:Android: Hinzufügen eines Geburtstagsereignisses zu einem Android-Kontakt programmgesteuert

String where = ContactsContract.Data.LOOKUP_KEY + " = ? AND " 
      + ContactsContract.Data.MIMETYPE + " = ?"; 
String[] params = new String[] { lookupKey, 
      ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE }; 

Cursor cursor = contentResolver.query(
      ContactsContract.Data.CONTENT_URI, null, where, params, null); 
if (cursor.moveToFirst()) { 
     birthdayRow = cursor.getInt(idIdx); 
     long rawContactId = cursor.getLong(cursor 
       .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); 
} 

Das Problem ist, dass, wenn es nicht Geburtstagsereignis für einen Kontakt gesetzt dann ist dieser cursor, den ich erhalte, leer ... und ich weiß nicht, wie man dieses Ereignis ohne eine raw_contact_id einfügt. Um das Ereignis einfügen i die Folowing tun:

values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); 
values.put(ContactsContract.Data.MIMETYPE, Event.CONTENT_ITEM_TYPE); 
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, 
birthdayStartDate); 
values.put(ContactsContract.CommonDataKinds.Event.TYPE, 
      ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY); 
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, 
      context.getString(R.string.birthday_label)); 
if (birthdayRow >= 0) { 
     int result = contentResolver.update(
       ContactsContract.Data.CONTENT_URI, values, 
       ContactsContract.Data._ID + " = " + birthdayRow, null); 
     Log.i("ContactList", "update result: " + result); 
} else { 
     Uri result = contentResolver.insert(
       ContactsContract.Data.CONTENT_URI, values); 
     Log.i("ContactList", "update result: " + result); 
} 

Also bitte informieren Sie uns, was soll ich tun, ist es eine Möglichkeit, um dieses Ereignis zu den Kontaktdaten whitout ein raw_contact id hinzufügen? Auch finde ich merkwürdig die Tatsache, dass für andere ContactData wie Nickname ich das gleiche mache und ich bekomme keinen leeren Cursor für die Parameter String [] params = new String [] {String.valueOf (lookupKey), ContactsContract.CommonDataKinds .Nickname.CONTENT_ITEM_TYPE}; , auch wenn der Kontakt keinen Spitznamen hat.

Antwort

0

Verwenden Sie diese Option, um die rohe Kontakt-ID vor dem Einfügen zu erhalten.

long rawContactId = -1; 
    String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID}; 
    String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=?"; 
    String[] selectionArgs = new String[]{ 
    String.valueOf(bdayContact.getId()) }; 
    Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null); 
    try { 
     if (c.moveToFirst()) { 
      rawContactId = c.getLong(0); 
     } 
    } finally { 
     c.close(); 
    } 
Verwandte Themen