2011-06-24 68 views
11

有誰知道該怎麼做?即使鏈接到一些教程會有所幫助。如何更新Android中聯繫人的電子郵件,地址,暱稱等?

我在使用Android 2.1的設備。

+1

它已經吃了四天,但你沒有時間寫整個句子... –

+0

我想說的是,如果你已經花了4天的時間解決這個問題,然後試圖問一個可讀的問題可以提高你獲得答案的機會。您在問題中出現的語法錯誤和拼寫錯誤數量使您看起來像沒有花時間。 –

+0

如果令人沮喪的是我的目標,我會簡單地低估或投票結束(這兩者我都沒有做)。我試圖在這裏幫助:寫清晰,可理解的語法(而不是使用「你」而不是「你」)將肯定會幫助你更加重視。如果有不少人跳過難以閱讀的語法問題,我不會感到驚訝。 –

回答

9
i found it myself.. thanks everyone... 


public void updateContact(String name, String phone,ContentResolver cr,Activity a) 
    { 

    String[] arr = {"DISPLAY_NAME","MIMETYPE","TYPE"}; 

    String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
       ContactsContract.Data.MIMETYPE + " = ? AND " + 
       String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? "; 
    String[] params = new String[] {name, 
      ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 
      String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)}; 

    Cursor phoneCur = a.managedQuery(ContactsContract.Data.CONTENT_URI, null, null, null, null); 
    for(int i = 0; i<phoneCur.getColumnCount()-1;i++) 
    { 
     if(phoneCur.moveToNext()) 
     { 
      String s = phoneCur.getString(phoneCur.getColumnIndex(Data.DISPLAY_NAME)); 
      String s1 = phoneCur.getString(phoneCur.getColumnIndex(Data.MIMETYPE)); 
      String s2 = phoneCur.getString(phoneCur.getColumnIndex(Phone.TYPE)); 
      if(s!=null) 
      { 
       Log.e("@@@@@@@@@@@@#############",s); 
      } 
      if(s1!=null) 
      { 
       Log.e("111111111111#############",s1); 
      } 
      if(s2!=null) 
      { 
       Log.e("2222222222222#############",s2); 
      } 
     } 
    } 

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 


     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
       .withSelection(where, params) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "5657") 
       // .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name 21") 
       .build()); 
     String where1 = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
     ContactsContract.Data.MIMETYPE + " = ?"; 
     String[] params1 = new String[] {name,"vnd.android.cursor.item/name"}; 
     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
       .withSelection(where1, params1) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name") 
       .build()); 


     String[] params2 = new String[] {name,"vnd.android.cursor.item/email_v2"}; 
     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
       .withSelection(where1, params2) 
       .withValue(ContactsContract.CommonDataKinds.Email.DATA, "Hi There") 
       .build()); 
    // phoneCur.close(); 

    try { 
     cr.applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (OperationApplicationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



} 
13

請查看以下鏈接

update contact details on Android


OR


創建XML文件 「editcontmain.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button android:layout_width="fill_parent" android:id="@+id/EditContacts" 
     android:onClick="@string/clickEvent" android:layout_height="wrap_content" 
     android:text="Edit Contacts" /> 
</LinearLayout> 

,並創建Java文件「EditContMain.java

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

public class EditContMain extends Activity { 

    private static final int PICK_CONTACT = 0; 
    private static final int EDIT_CONTACT = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.editcontmain); 

    } 

    public void clickHandler(View v) { 
     switch (v.getId()) { 
     case R.id.EditContacts: 
      Intent intent = new Intent(Intent.ACTION_PICK, 
        ContactsContract.Contacts.CONTENT_URI); 
      startActivityForResult(intent, PICK_CONTACT); 
      break; 
     default: 
      break; 
     } 
    } 

    @Override 
    public void onActivityResult(int reqCode, int resultCode, Intent data) { 
     super.onActivityResult(reqCode, resultCode, data); 

     switch (reqCode) { 
     case (PICK_CONTACT): 

      if (resultCode == Activity.RESULT_OK) { 

       Uri contactData = data.getData(); 
       Cursor c = managedQuery(contactData, null, null, null, null); 

       Log.e("c", c.getCount() + ""); 
       if (c.moveToFirst()) { 

        String contactId = c.getString(c 
          .getColumnIndex(ContactsContract.Contacts._ID)); 

        Toast.makeText(this, "ID = " + contactId, Toast.LENGTH_LONG) 
          .show(); 

        Intent i = new Intent(Intent.ACTION_EDIT); 
        i.setData(Uri 
          .parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI 
            + "/" + contactId)); 
        startActivityForResult(i, EDIT_CONTACT); 
       } 
      } 

      break;  
     } 
    } 
} 
+0

你是否確定它的日期聯繫..?或只是得到現有的.. – ngesh

+0

我看到了..但我會在哪裏設置我的價值..我問我怎麼沒有遇到過這種方式..可以告訴 – ngesh

+0

是的,它是更新現有的聯繫人。我有更新電子郵件,名稱等...我檢查它正在工作。 – Nikhil

2

我更新我的電子郵件,電話號碼,姓氏和名字接觸,如果電子郵件和電話號碼沒有再加入使用rowId電子郵件和號碼也。

私人無效updateContact(){

if (getRawContactId(contactId).length() == 0) { 
     AppUtils.showToast(AddContactsActivity.this, getString(R.string.sorry_msg)); 
    } else { 
     String name = etFname.getText().toString()+ " "+etLname.getText().toString().trim(); 
     ContentResolver cr = 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>(); 


     ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
       .withSelection(where, nameParams) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, etFname.getText().toString().trim()) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, etLname.getText().toString().trim()) 
       .build()); 


     if (categoryContactData.getContact_mobile().length() > 0) { 
      ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
        .withSelection(where, numberParams) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etPhone.getText().toString().trim()) 
        .build()); 
     } else { 

      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        //.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId)) 
        .withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId)) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etPhone.getText().toString().trim()) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
        .build()); 

     } 

     if (categoryContactData.getContact_email().length() > 0) { 
      ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
        .withSelection(where, emailParams) 
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, etEmail.getText().toString().trim()) 
        .build()); 
     } else { 

      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId)) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, etEmail.getText().toString().trim()) 
        .build()); 

     } 
     try { 
      cr.applyBatch(ContactsContract.AUTHORITY, ops); 

      setResult(RESULT_OK); 
      finish(); 
    //  Toast.makeText(AddContactsActivity.this, "Updated the phone number of 'Sample Name' to: ", Toast.LENGTH_SHORT).show(); 
     } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (OperationApplicationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
+0

,你得到RowId使用聯繫人ID。 – bhoomika

+0

這個答案對於給定的問題是最完整的。 –

相關問題