random
random

Reputation: 10309

ContactsContract API - fetch display name and organization title

How can we fetch displayname and organization.data through ContactsContract APIs using impicit joins so that I can both these values in a single cursor?

Upvotes: 1

Views: 1486

Answers (3)

Mohit Verma
Mohit Verma

Reputation: 3025

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

Upvotes: 5

Manish Khot
Manish Khot

Reputation: 3037

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.

Upvotes: 0

Manish Khot
Manish Khot

Reputation: 3037

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)

Upvotes: 0

Related Questions