开发者

ContactsContract API - fetch display name and organization title

How can we fetch displayna开发者_Python百科me and organization.data through ContactsContract APIs using impicit joins so that I can both these values in a single cursor?


You can use this code to get the organization name and display name:

Cursor organizationNameCursor = cr.query(ContactsContract.Data.CONTENT_URI,new String[] {Organization.TITLE,Organization.DISPLAY_NAME}, ContactsContract.Data.CONTACT_ID + " = " + contactId + " AND ContactsContract.Data.MIMETYPE = '"
                + ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
                + "'",null,null);

        organizationNameCursor.moveToNext();

        organizationName.setText(organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TITLE))+" "+organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.DISPLAY_NAME)));


The ContactsContact data can only be fetched by using content providers which does not allow us to have explicit joins in the query.

You can however have both the values using a single query on Data database as follows:

Cursor c = getContentResolver().query(Data.CONTENT_URI,new String[] {StructuredName.DISPLAY_NAME,Organization.COMPANY}, Data..CONTACT_ID + " = " + contactId,null,null)


In that case you wont be able to get the values directly. You can very well fetch all the details using a single query by adding a parameter

Data.MIMETYPE IN (StructuredName,CONTENT_ITEM_TYPE, Organization.CONTENT_ITEM_TYPE)  

however you need to have that logic in your code which would reorder your data. Each MIMETYPE will fetch a separate record.

Similarly you can use RawContactsEntity for the same. It provides a join between Contacts and Data database internally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜