Google联系人姓名提取

时间:2011-08-07 00:26:42

标签: android

我想通过其id.My代码获取联系人的姓名,如下所示:

public void Get_Contact_Name(String id)
{
    String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
            ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
            ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
            ContactsContract.CommonDataKinds.StructuredName.PREFIX,
            ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};

    String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};

    Cursor contacts = contentresolver.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

    if (contacts.moveToFirst()) { 
        contactcontent.contact_display_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME))));
        contactcontent.contact_given_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME))));
        contactcontent.contact_suffix_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX))));
        contactcontent.contact_prefix_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX))));
        contactcontent.contact_middle_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME))));
        contactcontent.contact_family_name = Null_check((contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))));
    } 
    contacts.close();

}

它没有错误,但跳过了方法的步骤。我认为问题在于:

    String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};

    Cursor contacts = contentresolver.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

有人可以查一下吗?

提前致谢。 问候 riskycoder

0 个答案:

没有答案