2012-03-27 93 views
0

我正在嘗試讓某人在特定聯繫號碼中編號主號碼。這是代碼:Android更改號碼爲主號碼

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); 
} 

然後我不斷收到如下:

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... 

我檢查了第一個查詢,它的工作,我能找到的解析器的數目,但無法更新。

+0

檢查此:http://stackoverflow.com/questions/3171482/android-edit-contact – Araw 2012-03-27 06:42:10

+0

看着這之前,有沒有幫助嘗試更新號碼本身,這都是關於查詢,謝謝反正:) – shimiDroid 2012-03-27 15:52:13

回答

0

好好解決後,看看解析器類。 {ArrayList(new ArrayList();});}}}}}}}}}}} 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; 
      } 

    } 

從開發指南中,我們可以看到:

概述

ContactsContract定義的聯繫人相關信息的擴展的數據庫。聯繫信息存儲在一個三層數據模型:

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. 

所以我的第一種方法是錯了,我不知道該datakind類可以代表相同的數據只針對我們的上下文中的數據類看看