2011-12-30 94 views
0

我已經編寫了簡單更新的代碼。我能夠從我的AVD的聯繫人中插入和刪除數據,但是當我想更新數據時,它沒有更新。Android:無法通過編程更新我的聯繫人

這裏是我的示例代碼:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
      String rawContactInsertIndex = (Integer.toString(ops.size())); 

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
         .withSelection(Data.CONTACT_ID + "=?" , new String[] { rawContactInsertIndex })       
        .withValue(StructuredName.DISPLAY_NAME, firstname) 
        .withValue(StructuredName.FAMILY_NAME, lastname) 
        .withValue(StructuredName.GIVEN_NAME, firstname) 

任何一個可以幫助?我被困在這裏。

+0

是否在您的manifest.xml中爲WRITE_CONTACTS添加了權限\t允許應用程序寫入(但不讀取)用戶的聯繫人數據。 – 2011-12-30 10:24:46

+0

亞我已添加 – shvivek 2011-12-30 10:27:19

回答

1

要開始,你.withSelection(Data.CONTACT_ID + "=?", new String[]{rawContactInsertIndex}是要在所有數據列,其中Data.CONTACT_ID列具有價值0進行更新。這包括電話號碼,地址等。

幸運的是,沒有這樣的接觸顯然,因爲你會f-ck聯繫了王室

首先,您應該獲取正確的CONTACT_ID並對您的選擇做一些工作,即在Data.MIME_TYPE上也做一個選擇。

contactId = Fetch the correct row identifier of the contact you want to update. 

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
    .withSelection(Data.CONTACT_ID + "=? AND " + Data.MIME_TYPE + "=?", 
     new String[]{contactId, StructuredName.CONTENT_ITEM_TYPE}) 
    .withValue(StructuredName.DISPLAY_NAME, firstName) 
    .withValue(StructuredName.FAMILY_NAME, lastName) 
    .withValue(StructuredName.GIVEN_NAME, givenName).build()); 

此外,你確定你應該更新CONTACT_ID?考慮使用RAW_CONTACT_ID

+0

謝謝你jens as iam新增至stackoverflow – shvivek 2011-12-30 10:39:40

相關問題