2011-12-13 25 views
16

Tôi có một liên hệ hiện có, tôi cần phải thêm địa chỉ cơ quan vào địa chỉ liên hệ hiện có đó. Tôi đang sử dụng mã sau đây, nhưng nó không hoạt động.Cách cập nhật liên hệ hiện có?

String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + 
    "'" + " AND " + ContactsContract.CommonDataKinds.StructuredPostal.TYPE + "=?"; 
String[] phoneArgs = new String[] 
    {String.valueOf(ContactId), String.valueOf(
    ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)}; 
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
    .withSelection(selectPhone, phoneArgs) 
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, STREET) 
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, CITY) 
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, REGION) 
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, POSTCODE) 
    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, COUNTRY) 
    .build()); 
this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

Bất kỳ giải pháp nào cho điều này?

+0

Tôi đang gặp phải sự cố tương tự không có kết quả, có bất kỳ trợ giúp nào không? – user788511

+0

bất cứ ai xin vui lòng giúp đỡ ?? – user788511

Trả lời

0

Có lẽ bạn có thể sử dụng Intent and its ACTION_EDIT để có được người dùng của bạn Chỉnh sửa địa chỉ làm việc ...

+0

Đây không phải là lý tưởng, tôi đã thử nó trước nhưng nó không đáp ứng các yêu cầu của ứng dụng của tôi. – user788511

1

Cuối cùng tôi tìm thấy solution..Much thích hợp nhờ đó How to modify existing Contact

Bí quyết là bạn phải vượt qua hai giá trị cho .withSelection như hình dưới đây:

.withSelection(Data.RAW_CONTACT_ID + " = ?", new String[] {String.valueOf(id)}) 
.withSelection(Data._ID + " = ?", new String[] {mDataId}) 

nơi bởi Data._ID giá trị mDataId thu được theo cách này:

Cursor mDataCursor = this.context.getContentResolver().query(
         Data.CONTENT_URI, 
         null, 
         Data.RAW_CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?", 
         new String[] { String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}, 
         null); 

       if(mDataCursor.getCount() > 0) { 
        mDataCursor.moveToFirst(); 
        mDataId = getCursorString(mDataCursor, Data._ID); 
        MLog.v("Data", "Found data item with MIMETYPE");        
        mDataCursor.close(); 

       } else { 
        MLog.v("Data", "Data doesn't contain MIMETYPE"); 
        result = ERROR; 
        mDataCursor.close(); 
       } 

Và phương pháp getCursorString là một cái gì đó như:

private static String getCursorString(Cursor cursor, String columnName) { 
     int index = cursor.getColumnIndex(columnName); 
     if(index != -1) return cursor.getString(index); 
     return null; 
    } 

này và chỉ đây là lừa ..

1

Mỗi trường (email, tên, adreess) có loại mime riêng của mình, mà bạn nên sử dụng để cập nhật trường.

Chúng tôi sẽ làm việc với Bảng dữ liệu, trong đó mỗi dữ liệu.RAW_CONTACT_ID thể hiện chi tiết về một số liên hệ.

Vì vậy, chúng tôi cần tìm dữ liệu.RAW_CONTACT_ID trong đó id là id của liên hệ bạn muốn chỉnh sửa.

Tôi hy vọng mã này sẽ hữu ích cho bạn.

String selectPhone = Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
         ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'" ; 
        String[] phoneArgs = new String[]{String.valueOf(rawContactId)}; 
        ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
         .withSelection(selectPhone, phoneArgs) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, STREET) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, CITY) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, REGION) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, POSTCODE) 
         .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, POSTCODE) 
         .build()); 
this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

Nếu một liên hệ mới đã được tạo nhưng không có địa chỉ và bây giờ bạn muốn thêm địa chỉ vào liên kết đó. Trong trường hợp này, hãy sử dụng cùng một truy vấn như trên, nhưng chỉ cần thay đổi newUpdate thành newInsert, vì hàng đó chưa tồn tại.

13
 /** 
      * @param name name of the contact 
      * @param number mobile phone number of contact 
      * @param email work email address of contact 
      * @param ContactId id of the contact which you want to update 
      * @return true if contact is updated successfully<br/> 
      *   false if contact is not updated <br/> 
      *   false if phone number contains any characters(It should contain only digits)<br/> 
      *   false if email Address is invalid <br/><br/> 
      *   
      * You can pass any one among the 3 parameters to update a contact.Passing all three parameters as <b>null</b> will not update the contact   
      * <br/><br/><b>Note: </b>This method requires permission <b>android.permission.WRITE_CONTACTS</b><br/> 
      */ 

      public boolean updateContact(String name, String number, String email,String ContactId) 
      { 
       boolean success = true; 
       String phnumexp = "^[0-9]*$"; 

       try 
       { 
         name = name.trim(); 
         email = email.trim(); 
         number = number.trim(); 

       if(name.equals("")&&number.equals("")&&email.equals("")) 
       { 
        success = false; 
       } 
       else if((!number.equals(""))&& (!match(number,phnumexp))) 
       { 
        success = false; 
       } 
       else if((!email.equals("")) && (!isEmailValid(email))) 
       { 
        success = false; 
       } 
       else 
       { 
        ContentResolver contentResolver = activity.getContentResolver(); 

        String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 

        String[] emailParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE}; 
        String[] nameParams = new String[]{ContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}; 
        String[] numberParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}; 

        ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>(); 

       if(!email.equals("")) 
       { 
        ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
          .withSelection(where,emailParams) 
          .withValue(Email.DATA, email) 
          .build()); 
       } 

       if(!name.equals("")) 
       { 
        ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
          .withSelection(where,nameParams) 
          .withValue(StructuredName.DISPLAY_NAME, name) 
          .build()); 
       } 

       if(!number.equals("")) 
       { 

        ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
          .withSelection(where,numberParams) 
          .withValue(Phone.NUMBER, number) 
          .build()); 
       } 
        contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); 
       } 
       } 
       catch (Exception e) 
       { 
       e.printStackTrace(); 
       success = false; 
       } 
       return success; 
      } 



    // To get COntact Ids of all contact use the below method 

    /** 
     * @return arraylist containing id's of all contacts <br/> 
     *   empty arraylist if no contacts exist <br/><br/> 
     * <b>Note: </b>This method requires permission <b>android.permission.READ_CONTACTS</b> 
     */ 
     public ArrayList<String> getAllConactIds() 
     { 
      ArrayList<String> contactList = new ArrayList<String>(); 

      Cursor cursor = activity.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, "display_name ASC"); 

       if (cursor != null) 
       { 
        if (cursor.moveToFirst()) 
        { 
         do 
         { 
          int _id = cursor.getInt(cursor.getColumnIndex("_id")); 
          contactList.add(""+_id); 

         } 
         while(cursor.moveToNext()); 
        } 
       } 

      return contactList; 
     } 


private boolean isEmailValid(String email) 
    { 
     String emailAddress = email.toString().trim(); 
     if (emailAddress == null) 
      return false; 
     else if (emailAddress.equals("")) 
      return false; 
     else if (emailAddress.length() <= 6) 
      return false; 
     else { 
      String expression = "^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)[email protected][a-z][a-z|0-9|]*\\.([a-z][a-z|0-9]*(\\.[a-z][a-z|0-9]*)?)$"; 
      CharSequence inputStr = emailAddress; 
      Pattern pattern = Pattern.compile(expression, 
        Pattern.CASE_INSENSITIVE); 
      Matcher matcher = pattern.matcher(inputStr); 
      if (matcher.matches()) 
       return true; 
      else 
       return false; 
     } 
    } 

    private boolean match(String stringToCompare,String regularExpression) 
    { 
     boolean success = false; 
     Pattern pattern = Pattern.compile(regularExpression); 
     Matcher matcher = pattern.matcher(stringToCompare); 
     if(matcher.matches()) 
      success =true; 
     return success; 
    } 
4

// Xin lỗi vì tôi xấu tiếng anh // Dường như trong bài viết đầu tiên bạn quên để thêm MimeType đi vào hoạt động.

String selectPhone = Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'" ; 
       String[] phoneArgs = new String[]{String.valueOf(rawContactId)}; 
       ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
        .withSelection(selectPhone, phoneArgs) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, STREET) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, CITY) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, REGION) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, POSTCODE) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, POSTCODE) 

**

// Chỉ cần thêm dòng này .withValue(Data.MIMETYPE, "vnd.android.cursor.item/postal-address_v2")

**

 .build()); 
this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

Vui lòng kiểm tra này và cho tôi biết kết quả

0

bạn nên sử dụng "Data.RAW_CONTACT_ID" thay vì "Data.CONTACT_ID" trong mệnh đề lựa chọn.

Các vấn đề liên quan