2012-03-27 120 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类可以代表相同的数据只针对我们的上下文中的数据类看看