基于组ID获得联络组联系人 [英] Get contacts from contact group based on group ID

查看:125
本文介绍了基于组ID获得联络组联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发现了一个伟大的 code 如何检索联系人从手机群:

I have a found a great code on how to retrieve contact groups from the phone:

final String[] GROUP_PROJECTION = new String[] {ContactsContract.Groups._ID, ContactsContract.Groups.TITLE };
Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,  null, ContactsContract.Groups.TITLE);
      while (cursor.moveToNext()) {

          String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));
          String gTitle = (cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE)));

          if (gTitle.contains("Group:")) {
            gTitle = gTitle.substring(gTitle.indexOf("Group:") + 6).trim();
          }
          if (gTitle.contains("Favorite_")) {
               gTitle = "Favorites";
          }
          if (gTitle.contains("Starred in Android") || gTitle.contains("My Contacts")) {
              continue;
          }
          arr_groups.add(gTitle);
          arr_groupswithid.add(id + "." + gTitle);
}    

的结果是:

我不知道为什么我得到不同的ID-S多同组。也许因为阿布舍克表明它是与存储在SIM卡,手机或从Facebook或Gmail同步联系人。

I have no idea why I get more of the same group with different id-s. Maybe as Abhishek suggested it has something to do with contacts stored on sim, phone or synced from Facebook or Gmail.

忽略这一点我一直在努力找回属于特定群组的联络人,但我从来没有得到正确的数字。

Ignoring this I have been trying to retrieve the contacts belonging to a specific group, but I never get the correct numbers.

String groupId = "10";
String where = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "="
               + groupId + " AND "
               + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
               + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'";

Cursor c = getContentResolver().query(
               ContactsContract.Data.CONTENT_URI,
               new String[] {
                       ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
                       ContactsContract.Data.DISPLAY_NAME
               }, where, null, ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

Log.i("cursorc", c.getCount() + "");

在groupId == 10,我得到7联系人。如果是== 12,我得到11如果是== 5,我得到0。总而言之18,这是很好的,因为我有同事组18人,见下图。

If the groupId==10, I get 7 contacts. If it is ==12, I get 11. If it is ==5, I get 0. All in all 18, which is good, as I have 18 persons in the Coworkers group, see image below.

在groupId == 3,我得到0的接触。如果是== 9,我得到2.如果是== 13,我得到0。总而言之2,这是很好的。

If the groupId==3, I get 0 contacts. If it is ==9, I get 2. If it is ==13, I get 0. All in all 2, which is good.

在groupId == 1,我得到0的接触。到底在哪儿是我的最爱?这应该给我2。

If the groupId==1, I get 0 contacts. Where the hell are my Favorites?? It should give me 2.

在groupId == 6,我得到0的接触。到底在哪儿是我的常用联系人?这应该给我17。

If the groupId==6, I get 0 contacts. Where the hell are my Frequent Contacts?? It should give me 17.

这是我组我的电话:

我不知道现在该做什么。

I don't know what to do now.

推荐答案

您群体ACCOUNT_TYPE是不同的,因此你会得到相同的名称与应不同的ID和不同的罪名,所以你应该确定你的类型,你想获得小组它的类型在您的选择,例如你可以选择切换到

your groups ACCOUNT_TYPE is different so you get same name with should different ids and different counts so you should determine your type that you want to get group of it is type in your selection for example you can change your selection to

final String GROUP_SELECTION = ContactsContract.Groups.AUTO_ADD + " = 0 "
                + " AND " + ContactsContract.Groups.ACCOUNT_TYPE + " = 'com.google' "
//              + " AND " + ContactsContract.Groups.ACCOUNT_TYPE + " = 'DeviceOnly' "
//              + " AND " + ContactsContract.Groups.ACCOUNT_TYPE + " = 'vnd.sec.contact.phone' "
                + " AND " + ContactsContract.Groups.SUMMARY_WITH_PHONES + " > 0 "
                + " AND " + ContactsContract.Groups.ACCOUNT_NAME + " NOT NULL  "
                + " AND " + ContactsContract.Groups.FAVORITES + " = 0 "
                + " AND " + ContactsContract.Groups.DELETED + " = 0  ";

这只能说明您的团体有com.google因为ACCOUNT_TYPE

this only show your groups that have com.google as there ACCOUNT_TYPE

这篇关于基于组ID获得联络组联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆