Need to fetch the Contact Account name and Contact Display name from the content provider using a single cursor in Android
I need to list the 'Display name' of all the contacts belonging to a single account.
I am planning to use a cursor adapter for populating the list and hence would require the corresponding fields (Display name, Account info) to be fetched in a single content provider query.
Looking at the Contacts Database Structure a single table among Raw Contacts, Contacts and Data wont be able to provide these two parameters and hence a join 开发者_开发技巧is necessary.
As far as i know join wont be possible to fetch the data from the content provider.
Can you please let me know the solution to the problem?
Thanks in advance.
Here is the code (this will fill cur2 with all the name information for every contact in accounts[0].name which is just the string name of the account and it will sort it by first name):
cur2 = getContentResolver().query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] {accounts[0].name}, DataQuery.COLUMN_GIVEN_NAME + " ASC");
private interface DataQuery {
public static final String[] PROJECTION =
new String[] {Data._ID, Data.MIMETYPE, Data.DATA1, Data.DATA2,
Data.DATA3, Data.DATA4, Data.DATA5, Data.DATA6, Data.DATA7,
Data.DATA8, Data.DATA9, Data.DATA10, Data.DATA11, Data.DATA12,
Data.DATA13, Data.DATA14, Data.DATA15, ContactsContract.RawContacts.ACCOUNT_NAME };
public static final int COLUMN_ID = 0;
public static final int COLUMN_MIMETYPE = 1;
public static final int COLUMN_DATA1 = 2;
public static final int COLUMN_DATA2 = 3;
public static final int COLUMN_DATA3 = 4;
public static final int COLUMN_DATA4 = 5;
public static final int COLUMN_DATA5 = 6;
public static final int COLUMN_DATA6 = 7;
public static final int COLUMN_DATA7 = 8;
public static final int COLUMN_DATA8 = 9;
public static final int COLUMN_DATA9 = 10;
public static final int COLUMN_DATA10 = 11;
public static final int COLUMN_DATA11 = 12;
public static final int COLUMN_DATA12 = 13;
public static final int COLUMN_DATA13 = 14;
public static final int COLUMN_DATA14 = 15;
public static final int COLUMN_DATA15 = 16;
public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
public static final int COLUMN_PHONE_TYPE = COLUMN_DATA2;
public static final int COLUMN_EMAIL_ADDRESS = COLUMN_DATA1;
public static final int COLUMN_EMAIL_TYPE = COLUMN_DATA2;
public static final int COLUMN_GIVEN_NAME = COLUMN_DATA2;
public static final int COLUMN_MIDDLE_NAME = COLUMN_DATA5;
public static final int COLUMN_FAMILY_NAME = COLUMN_DATA3;
public static final int COLUMN_WEBSITE = COLUMN_DATA1;
public static final int COLUMN_ADDRESS_TYPE = COLUMN_DATA2;
public static final int COLUMN_STREET_ADDRESS = COLUMN_DATA4;
public static final int COLUMN_CITY_ADDRESS = COLUMN_DATA7;
public static final int COLUMN_STATE_ADDRESS = COLUMN_DATA8;
public static final int COLUMN_ZIP_ADDRESS = COLUMN_DATA9;
public static final int COLUMN_ORGANIZATION_NAME = COLUMN_DATA1;
public static final int COLUMN_TITLE = COLUMN_DATA4;
public static final int COLUMN_PHOTO = COLUMN_DATA15;
public static final int COLUMN_NOTES = COLUMN_DATA1;
public static final String SELECTION = ContactsContract.RawContacts.ACCOUNT_NAME + "=? and " + Data.MIMETYPE + "='vnd.android.cursor.item/name'";
//public static final String a = Data.
}
You may request on ContactsContract.RawContacts with a selection on your account name and then on the ContactsContract.RawContacts.data table with a selection on the mime type CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
.
精彩评论