Reputation: 6810
I have an app that can add and delete contacts just fine. And if there is an existing value in an existing contact, it can be modified just fine. But I can't seem to be able to insert new values into existing contacts. For example, if there is an existing value for a home phone number, but not for the work phone number, I tried using the fillowing to add the value (cintact idValue
and workNumber
are passed in):
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
But I get a NullPointerException:
java.lang.NullPointerException
at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
at android.os.Binder.execTransact(Binder.java:336)
at dalvik.system.NativeStart.run(Native Method)
Can someone please tell me what I'm doing wrong?
Upvotes: 4
Views: 7599
Reputation: 41
You were getting error because Android doesn't give you access to add a contact directly to you Contact table. Rather you need to modify or add a raw Contact and Android will automatically create a Contact for you.
Upvotes: 1
Reputation: 4260
You are missing withValue(Phone.TYPE, Phone.TYPE_WORK)
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber).
withValue(Phone.TYPE, Phone.TYPE_WORK)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Edits
You are correct I checked the schema for the data
table and it is as follow it is only using raw contacts id and not contact id
CREATE TABLE data (_id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER REFERENCES package(_id),
mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
is_primary INTEGER NOT NULL DEFAULT 0,
is_super_primary INTEGER NOT NULL DEFAULT 0,
data_version INTEGER NOT NULL DEFAULT 0,
data1 TEXT,
data2 TEXT,
data3 TEXT,
data4 TEXT,
data5 TEXT,
data6 TEXT,
data7 TEXT,
data8 TEXT,
data9 TEXT,
data10 TEXT,
data11 TEXT,
data12 TEXT,
data13 TEXT,
data14 TEXT,
data15 TEXT,
data_sync1 TEXT,
data_sync2 TEXT,
data_sync3 TEXT,
data_sync4 TEXT );
Upvotes: 6