当前位置: 动力学知识库 > 问答 > 编程问答 >

add contact to a group in Android

问题描述:

I am working on android apps. I want to add a contact in android phone group. The code I am using is below:

 ContentValues values = new ContentValues();

values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,personId);

values.put(

ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,GROUP_ID);

values.put(

ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);

Log.d("values :", ""+ values);

this.getContentResolver().insert(

ContactsContract.Data.CONTENT_URI, values);

Unfortunately, this is not working. Does anyone see anything obviously wrong with the above code?

网友答案:

Bellow code is worked perfect in my side. So, Please try it or you can download sample example from chetanbhalala

try
{

   // Add selected contact to selected group
   ContentValues values = new ContentValues();
       values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,Integer.parseInt(245)); // 245 is a contact id, replace with selected contact id

       values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,3);// 3 is a group id, replace with selected group id

       values.put(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);

       ContextWrapper context = this;
       context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
       // End add contact to group code
}
catch (Exception e) 
{
   // TODO: handle exception
   Log.d("add group error :", ""+ e.getMessage().toString());
}
网友答案:

there is a issue in chetanbhalala / Add-contact-to-group-in-android with selected item on click function.

i solved it like this way

    @Override
public void onClick(View v) {
    if(v == btn_add)
    {
        if(selected_items_id.size() > 0)
        {
            selected_items_id.clear();
        }
        ListView parent = getListView();
        SparseBooleanArray choices = parent.getCheckedItemPositions();
        for (int i = 0; i < parent.getCount(); i++)
        {                
            if (choices.get(i))
            {
                selected_items_id.add(""+ group_contacts_id.get(i));
                  System.out.println(group_contacts_id.get(i)+"group_contacts_id is");
            }  
        } 

        if(selected_items_id.size() > 0)
        {
            add_group();
        }
        else
        {
            Toast.makeText(this, "please select contact:", Toast.LENGTH_LONG).show();
        }
    }
}
网友答案:
    Intent intent = new Intent(Intent.ACTION_INSERT);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);

                intent.putExtra(ContactsContract.Intents.Insert.NAME, fullname);
intent.putExtra(ContactsContract.Intents.Insert.PHONE,phoneno);
                intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
                intent.putExtra(ContactsContract.Intents.Insert.NOTES,
                        "Imported from moodle");
                intent.putExtra(ContactsContract.Intents.Insert.POSTAL, city
                        + " " + country);

                startActivityForResult(intent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case 1:
                if (requestCode == 1) {

                }
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: