dadrm33
dadrm33

Reputation: 43

getContentResolver().delete doesn't work

I'm trying to delete a specific contact! I've searched a lot, but every code I try doesn't work!

public void delete(String name,String numero)
{
Cursor cur = getContentResolver().query(Contacts.CONTENT_URI,null, null, null, null);

while (cur.moveToNext())
{
    try
    {
        String[] selectionArgs=new String[]{String.valueOf(numero)};
        String lookupKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
        Uri uri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
        getContentResolver().delete(uri, Phone.NUMBER +"=?", selectionArgs);
    }
    catch(Exception e)
    {
       System.out.println(e.getStackTrace());
    }
}

}

I also tried this instead of specifying selectionArgs but nothing

getContentResolver().delete(uri, Contacts.DISPLAY_NAME +"="+ name, null);

and tried to change Contacts. with ContactsContract.Contacts. but nothing and yet tried deleting with the Contact displayname instead of number. It seems that selection doesnt work! The uri value is Uri$HierarchicalUri is this right?

What's the issue?

Upvotes: 4

Views: 4516

Answers (1)

David Corsalini
David Corsalini

Reputation: 8208

Change

getContentResolver().delete(uri, Contacts.DISPLAY_NAME +"="+ name, null);

to

String[] names = {name};
getContentResolver().delete(uri, Contacts.DISPLAY_NAME +"=?", names);

Upvotes: 1

Related Questions