I'm working on Android contact. I want to query phone numbers (not contact name) from a specific group name. What query should i perform in order to do this?
Asked
Active
Viewed 1,252 times
1 Answers
0
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}
, null);
Then loop through cursor and get data u want. This will return data blocks showing the contactID and the groupID and other info. With this then query ContactsContract.Groups and get data about the group to compare.
If you are looking for specific data about a group first query for group row ID than you can add that to the following cursor like so...
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=? AND "+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, rowID }
, null);
wrote code here so sorry for silly mistakes
You can find group id like so...
Cursor c = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups._ID}, ContactsContract.Groups.TITLE+"=?","myGroup", null);

Maurycy
- 3,911
- 8
- 36
- 44
-
1Thanks for the answer. I've tried the query above and i successfully got contact names for the given group, using c.getString(c.getColumnIndex(Data.DISPLAY_NAME)). But when i did the same to get phone numbers, using c.getString(c.columnIndex(CommonDataKinds.Phone.NUMBER)), the cursor seemed to return group Row ID, not phone numbers. That's weird... – M Rijalul Kahfi Dec 27 '11 at 23:29
-
thats because phone numbers are stored under a different data block / mimetype and it seems like you are trying to search for them under the same mimetype as groups. You need to do a seperate query and search for the Phone.CONTENT_ITEM_TYPE mimetype instead or you can do one query and check to see which the mimetype is and handle appropriately. If this seems confusing then you should really read this page http://developer.android.com/resources/articles/contacts.html as it is very informative. – Maurycy Dec 28 '11 at 06:03