1. Are you ready for the Galaxy S20? Here is everything we know so far!

Delete Contact

Discussion in 'Android Development' started by ibwev, Mar 24, 2019.

  1. ibwev

    ibwev Lurker
    Thread Starter

    I am using the following code to attempt to delete a contact. I have used these two sites as references https://developer.android.com/reference/android/provider/ContactsContract.Data and https://www.dev2qa.com/how-to-update-delete-android-contacts-programmatically. My code executes but does not delete the contact from the phone. Please help me understand what I am doing wrong. If possible, I would prefer to open the users default contact app with the intended contact to delete preselected so the user can use the default contact app to delete the contact.

    Code (Text):
    1.     public void sendRent(View view){
    2.  
    3.         String rent = rentET.getText().toString();
    4.  
    5.         if (deleteTenant){
    6.  
    7.             long dataID = getRawContactIdByName(address);
    8.             ArrayList<ContentProviderOperation> opsDataTable = new ArrayList<ContentProviderOperation>();
    9.             ArrayList<ContentProviderOperation> opsRawData = new ArrayList<ContentProviderOperation>();
    10.  
    11.             opsDataTable.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
    12.                     .withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(dataID)})
    13.                     .build());
    14.             opsRawData.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
    15.                     .withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(dataID)})
    16.                     .build());
    17.  
    18.             try {
    19.                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
    20.                         != PackageManager.PERMISSION_GRANTED) {
    21.                     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS},MY_PERMISSION_REQUEST_CALL_PHONE);
    22.                     return;
    23.                 }
    24.                 getContentResolver().applyBatch(ContactsContract.AUTHORITY, opsDataTable);
    25.                 getContentResolver().applyBatch(ContactsContract.AUTHORITY, opsRawData);
    26.             }catch (Exception e){
    27.                 e.printStackTrace();
    28.             }
    29.         }
    30.  
    31.         BackgroundWorker bw = new BackgroundWorker(this);
    32.         bw.execute("RentEnter", rent, address, gid);
    33.  
    34.         finish();
    35.     }
    36.  
    37.     /* Get raw contact id by contact given name and family name.
    38.      *  Return raw contact id.
    39.      *  https://www.dev2qa.com/how-to-update-delete-android-contacts-programmatically/
    40.      * */
    41.     private long getRawContactIdByName(String contactName)
    42.     {
    43.         ContentResolver contentResolver = getContentResolver();
    44.  
    45.         // Query raw_contacts table by display name field ( given_name family_name ) to get raw contact id.
    46.  
    47.         // Create query column array.
    48.         String queryColumnArr[] = {ContactsContract.RawContacts._ID};
    49.  
    50.         // Create where condition clause.
    51.         String whereClause = ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY + " REGEXP '" + contactName + ".*'";
    52.         //String whereClause = ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY + " REGEXP '1 abc - y u'";
    53.  
    54.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
    55.                 != PackageManager.PERMISSION_GRANTED) {
    56.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},MY_PERMISSION_REQUEST_CALL_PHONE);
    57.         }
    58.         // Query raw contact id through RawContacts uri.
    59.         Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI;
    60.  
    61.         // Return the query cursor.
    62.         Cursor cursor = contentResolver.query(rawContactUri, queryColumnArr, whereClause, null, null);
    63.  
    64.         long rawContactId = -1;
    65.  
    66.         if(cursor!=null)
    67.         {
    68.             // Get contact count that has same display name, generally it should be one.
    69.             int queryResultCount = cursor.getCount();
    70.             // This check is used to avoid cursor index out of bounds exception. android.database.CursorIndexOutOfBoundsException
    71.             if(queryResultCount > 0)
    72.             {
    73.                 // Move to the first row in the result cursor.
    74.                 cursor.moveToFirst();
    75.                 // Get raw_contact_id.
    76.                 rawContactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
    77.             }
    78.         }
    79.  
    80.         return rawContactId;
    81.     }
     


  2. ibwev

    ibwev Lurker
    Thread Starter

    I found my error. I never used ContactsContract.Data.RAW_CONTACT_ID as my selection.

    It should have been:

    Code (Text):
    1.     [/COLOR][/LEFT]
    2. opsDataTable.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
    3.         .withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(dataID)})
    4.         .build());
    5. opsRawData.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
    6.         .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(dataID)})
    7.         .build());
    8.  
    9. [LEFT][COLOR=rgb(55, 71, 79)]    }
    [/QUOTE]

     
  3. lindamelody

    lindamelody Lurker

    Actually, we can delete the contact with hitting the "Delete" button. It is so difficult to remove the contacts with your steps.
     
Loading...

Share This Page

Loading...