0

我能够获取联系人的其他信息(显示名称、组织、电话号码和 email_id),但无法获取该联系人的生日和周年纪念日。

这是我用于生日的代码。它确实获取了数据,但给了我错误的数据,即为所有联系人重复相同的数据。

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

周年纪念日也是如此,这是它的代码。在某些情况下,不会添加周年纪念日,但仍会显示来自其他联系人的数据。

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;
}
4

1 回答 1

0

您没有在 where 条件下使用 String id 参数,因此请再次检查。

例如,私有 String getAnnv(String id) 函数具有 ID 输入,但似乎没有与函数一起使用,因此请将该 ID 用于条件检查,这应该可以工作。

例如

ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=  " +  ID
AND ContactsContract.Data.MIMETYPE + "= ? AND "
于 2012-07-30T13:21:49.517 回答