user4875712
user4875712

Reputation:

Adding Contact to new group in android

Hi im trying to create new gmail group and add contact to it.I'm successful in creating a group but contacts are not getting added to it.I have read many answers in stackoverflow but nothing worked.I cannot figure where i went wrong.I'm posting my code here please help.

Creating group

    public String createGroupInPhone() {

    String[] GROUP_PROJECTION = new String[]{ContactsContract.Groups._ID, ContactsContract.Groups.TITLE};
    ContentValues contentValues = null;

    try {


        ContentResolver cr = this.getContentResolver();
        contentValues = new ContentValues();

        contentValues.put(ContactsContract.Groups.TITLE, groupName);
        contentValues.put(ContactsContract.Groups.SHOULD_SYNC, true);
        contentValues.put(ContactsContract.Groups.GROUP_VISIBLE, 1);
        contentValues.put(ContactsContract.Groups.ACCOUNT_TYPE, "com.google");
        contentValues.put(ContactsContract.Groups.ACCOUNT_NAME, "[email protected]");
        cr.insert(ContactsContract.Groups.CONTENT_URI, contentValues);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String groupID;
    Cursor getGroupID_Cursor;
    getGroupID_Cursor = this.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[]{groupName}, null);

    getGroupID_Cursor.moveToFirst();
    groupID = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex(ContactsContract.Groups._ID)));
    String groupTitle = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex(ContactsContract.Groups.TITLE)));
    System.out.println("Group Title: " + groupTitle);
    getGroupID_Cursor.close();
    return groupID;
}

I have a doubt.How is this ContactsContract.Groups._ID related to GROUP_ROW_ID,GROUP_SOURCE_ID

adding Contact to new group

    public void addContactsToPhoneGroups(String contact_id, String groupId, String groupName) {

    System.out.println("ContactId: " + contact_id);
    System.out.println("GroupId: " + groupId);
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();

    ContentValues values = new ContentValues();}
    values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
            contact_id);
    values.put(
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
            groupId);
    values
            .put(
                    ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
                    ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);

     getContentResolver().insert(
            ContactsContract.Data.CONTENT_URI, values);

Upvotes: 1

Views: 725

Answers (2)

Ali Bagheri
Ali Bagheri

Reputation: 3429

1)

public static void createNewGroup(String name,@Nullable String accountName, @Nullable String accountType)
    {
        try
    {
        ContentValues groupValues = new ContentValues();
        groupValues.put(Groups.TITLE, name);
        groupValues.put(Groups.GROUP_VISIBLE, 1);
        //groupValues.put(Settings.UNGROUPED_VISIBLE, true);
        if(accountType != null && accountName != null)
        {
            groupValues.put(Groups.ACCOUNT_TYPE, accountType);
            groupValues.put(Groups.ACCOUNT_NAME, accountName);
            groupValues.put(Groups.SHOULD_SYNC, true);

        }
        else
        {
            groupValues.putNull(Groups.ACCOUNT_TYPE);
            groupValues.putNull(Groups.ACCOUNT_NAME);
            groupValues.put(Groups.SHOULD_SYNC, false);

        }
        getContentResolver().insert(Groups.CONTENT_URI, groupValues);
        return true;
    }
    catch (Exception e){}
    }

2)

public static Uri addContactToGroup(String rawContactId,String groupId)
    {
        try
        {
            ContentValues values = new ContentValues();
            values.put(Data.RAW_CONTACT_ID, rawContactId);
            values.put(GroupMembership.GROUP_ROW_ID, groupId);
            values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);

            return getContentResolver().insert(Data.CONTENT_URI, values);
        }
        catch (Exception e)
        {}
        return Uri.EMPTY;
    }

3)

public static Cursor getContactGroupId(String contactId)
    {
        Uri uri = ContactsContract.Data.CONTENT_URI;
        String[] columns = new String[] { GroupMembership.GROUP_ROW_ID };
        String where = Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?";
        String[] args = new String[] {contactId, GroupMembership.CONTENT_ITEM_TYPE };
        return Cursor contacts = getContentResolver().query(uri, columns, where, arg, null);
    }

4)

public static Cursor getGroupsList(@Nullable String[] project,@Nullable String where,@Nullable String[] args,@Nullable String sort)
    {

        return getContentResolver().query(Groups.CONTENT_URI, project, where, args, sort);
    }

Upvotes: 2

davidmc.w3sys
davidmc.w3sys

Reputation: 51

I see a lot of people posting examples where they do a subsequent query to get the id of the inserted element.

But the return from the insert is the Uri of the inserted row.

Why waste a call to the db?

long id = -1;
Uri insertedUri = getContentResolver().insert(...);
if (insertedUri!=null) {
   id = ContentUris.parseId(insertedUri);
}

Upvotes: 0

Related Questions