java - Getting Contacts based on the GroupID in Android -
i'm having kind of problem here, since startet developing android. downloaded sample official android website "http://developer.android.com/training/contacts-provider/retrieve-names.html", capable of retrieving showing contacts phone. feature wanted add show contacts group "friends" (hardcoded).
as far narrowed down have change selection part
final static string selection = (utils.hashoneycomb() ? contacts.display_name_primary : contacts.display_name) + "<>''" + " , " + contacts.in_visible_group + "=1";
to this
final static string selection = contacts.groupid = "friends";
which gives me errors, because can't find column.
i'm eager explore potential of android, 1 giving me headache.
there 2 ways getting list of contacts of group. first, suppose have groupid , want related list of contacts.
string[] projection = { contactscontract.groups._id, contactscontract.groups.title, contactscontract.groups.account_name, contactscontract.groups.account_type }; return context.getcontentresolver().query( contactscontract.groups.content_uri, projection, contactscontract.groups._id + "=" + groupid , null, null );
second way: suppose want contacts of specific group constants name. so, it's enough change above codes:
context.getcontentresolver().query( contactscontract.groups.content_uri, projection, contactscontract.groups.account_name + "='friends'" , null, null );
now have necessary details specific group. can fetch list of contact list:
public static cursor getcontactsofgroup(group group) { // getting ids of contacts in specific group string = contactscontract.commondatakinds.groupmembership.group_row_id + "=" + group.id + " , " + contactscontract.commondatakinds.groupmembership.mimetype + "='" + contactscontract.commondatakinds.groupmembership.content_item_type + "'"; cursor query = context.getcontentresolver().query( contactscontract.data.content_uri, new string[] { contactscontract.commondatakinds.groupmembership.contact_id }, where, null, contactscontract.data.display_name + " collate localized asc"); string ids = ""; (query.movetofirst(); !query.isafterlast(); query.movetonext()) { ids += "," + query.getstring(0); } if (ids.length() > 0) { ids = ids.substring(1); } // getting of information of contacts. fetches of number every 1 string[] projection = new string[]{ "_id", "contact_id", "lookup", "display_name", "data1", "photo_id", "data2", // number type: 1:home, 2:mobile, 3: work, else : other }; string selection = "mimetype ='" + contactscontract.commondatakinds.phone.content_item_type + "'" + " , account_name='" + group.accountname + "' , account_type='" + group.accounttype + "'" + " , contact_id in (" + ids + ")"; return context.getcontentresolver().query(base_uri, projection, selection, null, null); }
notice, in second fetch in method check accountname , accounttype sure record related group, because may there records stored apps whatsapp. , don't those. ok?
i hope useful you.
Comments
Post a Comment