0

I want to create a contact in the device's address book. The answer to the question here and the documentation indicates that we must provide the ID for the contact.

Here is the example from the docs:

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

How can I choose/generate this ID ? if I choose a random value, it might conflict with an existing contact ?

Community
  • 1
  • 1
Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • is there a reason why you have revert (unaccepted) the answer? if there is something wrong, i would like to know :) thanks – Yazan Dec 10 '14 at 09:09
  • @Yazan I'm still testing. I definitely don't understand what this CONTACT_ID is for. On iOS, when you create a contact, you don't need any ID. The ID is provided to you by the API once the contact has been created. – Arnaud Dec 10 '14 at 09:29
  • yes it's little bit confusing, check this answer, where it explains some points numbered 1,2,3 ... etc, it could help you understand, keep in mind, the structure of Android could be different than IOS, it's not necessary to be the same logic and structure... check this: http://stackoverflow.com/questions/5151885/android-new-data-record-is-added-to-the-wrong-contact/5158059#5158059 – Yazan Dec 10 '14 at 09:34
  • also i have edited the answer, adding more details this could help you – Yazan Dec 10 '14 at 09:39

2 Answers2

2

if you looked at the Documentation you will find a table describes all fields, among them is RAW_CONTACT_ID, i quote

The id of the row in the ContactsContract.RawContacts table that this data belongs to.

so this is the ID of an inserted record at RawContacts table, as if it's master detail schema check this link for more info about RawContacts

so what i think is you have to insert a rawContact first, get that (automatically generated id) and use it for inserting a contact

something like this

ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long insertedRawContactId = ContentUris.parseId(rawContactUri);

then use insertedRawContactId to insert contact.

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, insertedRawContactId );
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

check this site for more info to make it clear. and regarding the 3 types or 3 elements of contact, check this image Android Contacts...

and check this answer points 1,2,3,... etc

hope this help you more understanding

Community
  • 1
  • 1
Yazan
  • 6,074
  • 1
  • 19
  • 33
0

try like this,

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactInsertIndex);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

hope it will help you

Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19