Android - Trouble deleting only contacts with a specified note

146 views Asked by At

I am working on a project that creates random contacts but I am having trouble implementing the deletion capability. I have marked each generated contact with a note through

        ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE)
            .withValue(Note.NOTE, note)
            .build());

Now to delete the contacts i need to use something similar to what i have found here

Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                           Uri.encode(phoneNumber));
       Cursor cur = ctx.getContentResolver().query(contactUri, null, null,
                       null, null);
       try {
           if (cur.moveToFirst()) {
               do {
                  String lookupKey = 
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                  Uri uri = Uri.withAppendedPath(
                                 ContactsContract.Contacts.CONTENT_LOOKUP_URI,
                                 lookupKey);
                  ctx.getContentResolver().delete(uri, null, null);
               } while (cur.moveToNext());
           }

       } catch (Exception e) {
               System.out.println(e.getStackTrace());
       }
       return false;
}

I believe the only difference between the code above and what I am searching for is that the one above uses a phone number to delete the contact but for my purposes I need to delete by the Note field of the contact.

1

There are 1 answers

0
JGray On BEST ANSWER

Fixed my problem..I am too new to java and tried comparing strings using '==' instead of stringObject.equals().

here is a little function to delete contacts based on their string in case anyone needs something like that.

public void deleteContacts(String contactNote){
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    String note;
    while (cur.moveToNext()) {
        note = "";
        try{

            String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            String noteWhere = ContactsContract.Contacts.LOOKUP_KEY + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            String[] noteWhereParams = new String[]{lookupKey,
                    ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
            Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
            if (noteCur.moveToFirst()) {
                note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                if(note.equals(contactNote)) {
                        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                        cr.delete(uri, null, null);
                }
            }
            noteCur.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getStackTrace());
        }
    }
    cur.close();
}