Android 删除组中的所有联系人

Android 删除组中的所有联系人,android,android-contacts,Android,Android Contacts,我正在尝试从定义的组中删除所有联系人,但我不知道如何从联系人表和组表中进行连接(如果可能的话) 当然,它给了我一个错误,因为联系人组中没有“标题”,但是如果我使用ID进行连接,我应该得到我想要的 知道如何加入吗?看起来很奇怪,因为Contacts contract.Contacts没有Contacts contract.Groups.TITLE列。因此,我认为您可以使用想要的组标题获取组id,然后使用组id搜索联系人。其想法可能如下所示: public String getGroupId

我正在尝试从定义的组中删除所有联系人,但我不知道如何从联系人表和组表中进行连接(如果可能的话)

当然,它给了我一个错误,因为联系人组中没有“标题”,但是如果我使用ID进行连接,我应该得到我想要的


知道如何加入吗?

看起来很奇怪,因为Contacts contract.Contacts没有Contacts contract.Groups.TITLE列。因此,我认为您可以使用想要的组标题获取组id,然后使用组id搜索联系人。其想法可能如下所示:

    public String getGroupIdByTitle(String groupTitle){ 
        try {
            cursor = mContentResolver.query(
                 ContactsContract.Groups.CONTENT_URI, 
                    new String[] {Groups._ID}, 
                    Groups.TITLE + "=?", 
                    new String[]{groupTitle}, 
                    null);

                    while (cursor.moveToNext()){
                       return cursor.getString(cursor.getColumnIndex(0);
                    }

        } finally {
            if (cursor!=null) cursor.close();
        }

        return "";
    }

    public String getGroupIdOfContact(String lookupKey) {

    String where = String.format("%s=? AND %s=?", Data.LOOKUP_KEY, Data.MIMETYPE);
    String[] whereArgs = {lookupKey, GroupMembership.CONTENT_ITEM_TYPE};

    String groupRowId = "";
        Cursor cursor = mContentResolver.query(
                Data.CONTENT_URI, 
                new String[]{GroupMembership.GROUP_ROW_ID}, 
                where, whereArgs, null);
        try {
            if (cursor.moveToNext()) {
              return cursor.getString(cursor.getColumnIndex(GroupMembership.GROUP_ROW_ID));
            } 

        } finally {
            if (cursor!=null) cursor.close();
        }

return "";
    }

    public void deleteContactByGroupTitle(String groupTitle) {

        String targetGroupId = getGroupIdByTitle(groupTitle);

        Cursor cursor = null;
        try {
            cursor = mContentResolver.query(Contacts.CONTENT_URI, null, null, null, null);

            while (cursor.moveToNext()){
            String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
                String groupId = getGroupIdOfContact(lookupKey);
                if (targetGroupId.equals(groupId)){
                       //TODO. delete this contact
                }
            }

        } finally {
            if (cursor!=null) cursor.close();
        }

    }
上面的代码还没有经过测试,但我认为基本思想是一样的

    public String getGroupIdByTitle(String groupTitle){ 
        try {
            cursor = mContentResolver.query(
                 ContactsContract.Groups.CONTENT_URI, 
                    new String[] {Groups._ID}, 
                    Groups.TITLE + "=?", 
                    new String[]{groupTitle}, 
                    null);

                    while (cursor.moveToNext()){
                       return cursor.getString(cursor.getColumnIndex(0);
                    }

        } finally {
            if (cursor!=null) cursor.close();
        }

        return "";
    }

    public String getGroupIdOfContact(String lookupKey) {

    String where = String.format("%s=? AND %s=?", Data.LOOKUP_KEY, Data.MIMETYPE);
    String[] whereArgs = {lookupKey, GroupMembership.CONTENT_ITEM_TYPE};

    String groupRowId = "";
        Cursor cursor = mContentResolver.query(
                Data.CONTENT_URI, 
                new String[]{GroupMembership.GROUP_ROW_ID}, 
                where, whereArgs, null);
        try {
            if (cursor.moveToNext()) {
              return cursor.getString(cursor.getColumnIndex(GroupMembership.GROUP_ROW_ID));
            } 

        } finally {
            if (cursor!=null) cursor.close();
        }

return "";
    }

    public void deleteContactByGroupTitle(String groupTitle) {

        String targetGroupId = getGroupIdByTitle(groupTitle);

        Cursor cursor = null;
        try {
            cursor = mContentResolver.query(Contacts.CONTENT_URI, null, null, null, null);

            while (cursor.moveToNext()){
            String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
                String groupId = getGroupIdOfContact(lookupKey);
                if (targetGroupId.equals(groupId)){
                       //TODO. delete this contact
                }
            }

        } finally {
            if (cursor!=null) cursor.close();
        }

    }