Tomasz Wójcik
Tomasz Wójcik

Reputation: 962

Android contacts: updating, deleting, inserting

i have problem with basic operations on the contact book, looks like official examples doesn't work for me. One of them:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                      .withValue(Data.RAW_CONTACT_ID, id)
                      .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                      .withValue(Phone.NUMBER, "1-800-GOOG-411")
                      .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                      .withValue(Phone.LABEL, "free directory assistance")
                      .build());

This should add a contact with the given id, but I don't get any new contacts after running this code in emulator.

I will really appreciate any guiding answer or link to the tutorials.

Upvotes: 1

Views: 1249

Answers (2)

NAP-Developer
NAP-Developer

Reputation: 3796

Just this code implement for your apps and try out for delete particular contacts list,

ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" , new String[] { contactsId(your) }, null);

                if (cur.getCount() > 0) {

                    while (cur.moveToNext()) {

                        try {
                            String lookupKey = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                            Uri uri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                            cr.delete(uri, null, null);
                            setFinish();
                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }
                }

Upvotes: 1

setzamora
setzamora

Reputation: 3640

you should have the following permissions:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

you should have the code (after your code):

try {
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
    e.printStackTrace();
}

this will try to insert / update the contact

Upvotes: 1

Related Questions