2

Ok, I'm going batty. I've been messing with this for a bit. Shouldn't deleting a single contact once you know the contact_id be this simple? This delete runs, throws no error, but doesn't delete the contact.

the Log information show something like:

delete    contact_id = '615'
delete    return code : 0

This is the code involved:

private static int deleteContact(Context c, String id) {

    String where = ContactsContract.Data.CONTACT_ID + " = '" + id + "'";
    int rc = -1;
    try {
        Log.i("delete", where);
        rc = c.getContentResolver().delete(ContactsContract.Contacts.CONTENT_URI, where, null);
        Log.i("delete",String.format("return code : %d", rc));
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }

    return rc;
}

Can someone help me find a solution or suggest an alternative?

phemt.latd
  • 1,775
  • 21
  • 33
mcmprch
  • 19
  • 3
  • first, are you sure that contact with this id exists? second, better option is to use `c.getContentResolver().delete(ContactsContract.Contacts.CONTENT_URI, ContactsContract.Data.CONTACT_ID + "=?", new String[] {id});` – Selvin Jan 23 '14 at 15:02
  • 1
    have you tried the method found at http://stackoverflow.com/questions/527216/how-to-remove-a-contact-programmatically-in-android ? – John Boker Jan 23 '14 at 15:03
  • The answer to the first is...yes. I am sure that ID exists. I (stupid me) chose to sync both outlook and google address books. I was hoping for a fairly intelligent sync, but not so much. Consequently I have some dups. The code i am using loops through the contacts examining the AccountType... if it's google, it stays, it its outlook the id is passed to my little function to get removed. I started with the code in SO#527216, cleaned it up to what i have. I did start with the suggested ( – mcmprch Jan 23 '14 at 18:42

1 Answers1

-1

Well, I went back and revisited questions/527216/... darned if it didn't work for me. I don't know what I was doing wrong all day... but a few hour break, some more copy/paste and this function deletes a single contact for me:

    private static int deleteContact(Context c, String lookupKey) {
    int rc = 0;
    ContentResolver cr = c.getContentResolver();
    try {
        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
        rc = cr.delete(uri, null, null);
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }

    return rc;
}

Thanks for the answers!

mcmprch
  • 19
  • 3