0

I'm trying to make someones number a primary number in the specific contact numbers. This is the code:

Cursor the_phone = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ numberToCall, null, null);

            ContentValues values = new ContentValues();
            if (the_phone.moveToFirst()){


                 values.put(ContactsContract.CommonDataKinds.Phone.LABEL,
                         the_phone.getString(the_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));


            values.put(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY,1);
            values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,numberToCall);


            int phones = _context.getContentResolver().update(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,values, ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ numberToCall, null);
 }

Then I keep getting the following:

03-27 08:18:27.009: E/AndroidRuntime(640): FATAL EXCEPTION: main
03-27 08:18:27.009: E/AndroidRuntime(640): java.lang.UnsupportedOperationException: URI: content://com.android.contacts/data/phones, calling user: com...

I checked the first query and it's working I'm able to find the number in the resolver but unable to update it.

user
  • 86,916
  • 18
  • 197
  • 190
shimiDroid
  • 201
  • 4
  • 9
  • Check this: http://stackoverflow.com/questions/3171482/android-edit-contact – Araw Mar 27 '12 at 06:42
  • looked at this before, there is no help about trying to update the number itself, it's all about query, thanks anyway :) – shimiDroid Mar 27 '12 at 15:52

1 Answers1

1

ok actually solved after looking into the resolver class.

private Boolean editPrimary( Cursor phones , String contactId, String contactNumber, int primaryTo){ ArrayList ops = new ArrayList();

        String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +
        ContactsContract.CommonDataKinds.Phone.MIMETYPE + " = ? AND " +
        String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? AND " +
        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?";

        String[] params = new String[] {contactId,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                String.valueOf(phones.getInt(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE))),
                contactNumber};

         ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(where, params)
                .withValue(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY,primaryTo)
                .build());


         try {
             _context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
             return true;    
            } catch (RemoteException e) {
                e.printStackTrace();
                return false;
            } catch (OperationApplicationException e) {
                e.printStackTrace();
                return false;
            }

    }

from the developer guide we can see that :

Overview

ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model:

A row in the ContactsContract.Data table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended. There is a predefined set of common kinds, but any application can add its own data kinds.
A row in the ContactsContract.RawContacts table represents a set of data describing a person and associated with a single account (for example, one of the user's Gmail accounts).
A row in the ContactsContract.Contacts table represents an aggregate of one or more RawContacts presumably describing the same person. When data in or associated with the RawContacts table is changed, the affected aggregate contacts are updated as necessary.

so my first approach was wrong i didn't realize that the datakind class could represent the same data as the data class only directed to the context we are looking at

shimiDroid
  • 201
  • 4
  • 9