2011-06-28 5 views
3

イム私の携帯電話に/更新の連絡先を挿入するためにnew'ish APIを使用して、私はそれらを更新するために、このメソッドを使用しています。..アンドロイドコンタクト - アップデート注意

String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?"; 
    String[] selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_OTHER)}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
      .withSelection(selectArgs1, selectArgs2) 
      .withValue(Note.Note, "[email protected]") 
      .build()); 

これは、すべてを除いて、正常に動作しています私がノートを更新するときに。そこそのようなNote.Type_1か何かではありませんので、私は非常にapreciatedされるものEmail.TYPEString.valueOf(Email.TYPE_OTHER)

のいずれかの助けを置き換えるでselectArgs1とselectArgs2に配置するかわかりません。ありがとう! :)

は、[編集]

public static void updateContact(Context c, int recordId, String key, String value) { 
    context = c; 
    if (database == null) database = new SavedContactsDatabase(context); 
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); 
    ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(Data.CONTENT_URI); 
    String contactId = String.valueOf(recordId); 
    String selectArgs1 = ""; 
    String[] selectArgs2 = null; 

    String type = ""; 
    String subkey = ""; 
    if (key.contains(":")) { 
     String[] split = key.split(":"); 
     key = split[0]; 
     type = split[1]; 
     if (split.length == 3) subkey = split[2]; 
    } 

    if (key.equals("Email")) { 
     if (type.equals("Home")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_HOME)}; 
     } else if (type.equals("Work")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_WORK)}; 
     } else if (type.equals("Mobile")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_MOBILE)}; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Email.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Email.DATA1, value); 
    } 

    if (key.equals("Nickname")) { 
     if (type.equals("Default")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_DEFAULT)}; 
     } else if (type.equals("Other Names")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_OTHER_NAME)}; 
     } else if (type.equals("Maiden Name")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_MAINDEN_NAME)}; 
     } else if (type.equals("Short Name")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_SHORT_NAME)}; 
     } else if (type.equals("Initials")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_INITIALS)}; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Nickname.CONTENT_ITEM_TYPE + "'" + " AND " + Nickname.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Nickname.NAME, value); 
    } 

    ////////////////////////////////////////////////////////////////////////////////////////////////////// 
    if (key.equals("Note")) { 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'"; 
     selectArgs2 = new String[]{contactId, "vnd.android.cursor.item/note"}; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Note.NOTE, "[email protected]"); 

    } 
    ///////////////////////////////////////////////////////////////////////////////////////////////////// 

    if (key.equals("Organization")) { 
     String data = ""; 
     if (type.equals("Company")) { 
      data = Organization.COMPANY; 
     } else if (type.equals("Title")) { 
      data = Organization.TITLE; 
     } else if (type.equals("Department")) { 
      data = Organization.DEPARTMENT; 
     } else if (type.equals("Job Descripiton")) { 
      data = Organization.JOB_DESCRIPTION; 
     } else if (type.equals("Symbol")) { 
      data = Organization.SYMBOL; 
     } else if (type.equals("Phonetic Name")) { 
      data = Organization.PHONETIC_NAME; 
     } else if (type.equals("Office Location")) { 
      data = Organization.OFFICE_LOCATION; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Organization.CONTENT_ITEM_TYPE + "'" + " AND " + Organization.TYPE + "=?"; 
     selectArgs2 = new String[]{contactId, String.valueOf(Organization.TYPE_WORK)}; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(data, value); 
    } 

    if (key.equals("Phone")) { 
     if (type.equals("Home")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_HOME)}; 
     } else if (type.equals("Mobile")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MOBILE)}; 
     } else if (type.equals("Work")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)}; 
     } else if (type.equals("Fax Work")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_FAX_WORK)}; 
     } else if (type.equals("Fax Home")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_FAX_HOME)}; 
     } else if (type.equals("Pager")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_PAGER)}; 
     } else if (type.equals("Other")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_OTHER)}; 
     } else if (type.equals("Callback")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_CALLBACK)}; 
     } else if (type.equals("Car")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_CAR)}; 
     } else if (type.equals("Company Main")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_COMPANY_MAIN)}; 
     } else if (type.equals("ISDN")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_ISDN)}; 
     } else if (type.equals("Main")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MAIN)}; 
     } else if (type.equals("Other Fax")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_OTHER_FAX)}; 
     } else if (type.equals("Radio")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_RADIO)}; 
     } else if (type.equals("Telex")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_TELEX)}; 
     } else if (type.equals("TTY TDD")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_TTY_TDD)}; 
     } else if (type.equals("Work Mobile")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK_MOBILE)}; 
     } else if (type.equals("Work Pager")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK_PAGER)}; 
     } else if (type.equals("Assistant")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_ASSISTANT)}; 
     } else if (type.equals("MMS")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MMS)}; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Phone.NUMBER, value); 
    } 

    //TODO Relation? 

    ////////////////////////////////////////////////////////////////////////////////////// 
    if (key.equals("Name")) { 
     String data = ""; 
     if (type.equals("Display Name")) { 
      data = StructuredName.DISPLAY_NAME; 
     } else if (type.equals("Given Name")) { 
      data = StructuredName.GIVEN_NAME; 
     } else if (type.equals("Family Name")) { 
      data = StructuredName.FAMILY_NAME; 
     } else if (type.equals("Prefix")) { 
      data = StructuredName.PREFIX; 
     } else if (type.equals("Middle Name")) { 
      data = StructuredName.MIDDLE_NAME; 
     } else if (type.equals("Suffix")) { 
      data = StructuredName.SUFFIX; 
     } else if (type.equals("Phonetic Given Name")) { 
      data = StructuredName.PHONETIC_GIVEN_NAME; 
     } else if (type.equals("Phonetic Middle Name")) { 
      data = StructuredName.PHONETIC_MIDDLE_NAME; 
     } else if (type.equals("Phonetic Family Name")) { 
      data = StructuredName.PHONETIC_FAMILY_NAME; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'" + " AND " + StructuredName.MIMETYPE + "=?"; 
     selectArgs2 = new String[]{contactId, String.valueOf(StructuredName.MIMETYPE)}; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(data, value); 
    } 
    //////////////////////////////////////////////////////////////////////////////////////// 

    if (key.equals("Address")) { 
     String data = ""; 
     if (type.equals("Home")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_HOME)}; 
     } else if (type.equals("Work")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_WORK)}; 
     } else if (type.equals("Other")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_OTHER)}; 
     } 
     if (subkey.equals("Street")) { 
      data = CommonDataKinds.StructuredPostal.STREET; 
     } else if (subkey.equals("PO Box")) { 
      data = CommonDataKinds.StructuredPostal.POBOX; 
     } else if (subkey.equals("Neighbourhood")) { 
      data = CommonDataKinds.StructuredPostal.NEIGHBORHOOD; 
     } else if (subkey.equals("City")) { 
      data = CommonDataKinds.StructuredPostal.CITY; 
     } else if (subkey.equals("Region")) { 
      data = CommonDataKinds.StructuredPostal.REGION; 
     } else if (subkey.equals("Postcode")) { 
      data = CommonDataKinds.StructuredPostal.POSTCODE; 
     } else if (subkey.equals("Country")) { 
      data = CommonDataKinds.StructuredPostal.COUNTRY; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'" + " AND " + CommonDataKinds.StructuredPostal.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(data, value); 
    } 

    if (key.equals("Website")) { 
     if (type.equals("Homepage")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_HOMEPAGE)}; 
     } else if (type.equals("Blog")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_BLOG)}; 
     } else if (type.equals("Profile")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_PROFILE)}; 
     } else if (type.equals("Home")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_HOME)}; 
     } else if (type.equals("Work")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_WORK)}; 
     } else if (type.equals("FTP")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_FTP)}; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Website.CONTENT_ITEM_TYPE + "'" + " AND " + Website.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Website.URL, value); 
    } 

    if (key.equals("Event")) { 
     if (type.equals("Anniversary")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Event.TYPE_ANNIVERSARY)}; 
     } else if (type.equals("Birthday")) { 
      selectArgs2 = new String[]{contactId, String.valueOf(Event.TYPE_BIRTHDAY)}; 
     } 
     selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + Event.CONTENT_ITEM_TYPE + "'" + " AND " + Event.TYPE + "=?"; 
     builder.withSelection(selectArgs1, selectArgs2); 
     builder.withValue(Event.START_DATE, value); 
    } 





    /* 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
      .withSelection(Data.RAW_CONTACT_ID + "=?", new String[] {sdf}) 
      .withValue(Email.DATA1, "work.hi.com") 
      .withValue(Email.TYPE, Email.TYPE_WORK) 
      .build()); 
    */ 
    /* 
    String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + 
    Email.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?"; 
    String[] phoneArgs = new String[]{contactId, String.valueOf(Email.TYPE_OTHER)}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
      .withSelection(selectPhone, phoneArgs) 
      .withValue(Email.DATA1, "[email protected]") 
      .build()); 
    */ 

    try { 
     operationList.add(builder.build()); 
     ContentProviderResult[] res = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList); 
     if (res[0].count == 0) { 
      //TODO Then Use the Insert Command Instead! 
     } 
    } catch (Exception e) { Log.d("fhksdsdj", e.toString()); } 
} 
+0

...このコードを試してみてくださいUは、質問は、私は同様の問題を持っていたあなたは手の込んだことができれば私は...それを固定あまり明確ではありませんrはそれはvを助けることができます.. – ngesh

+0

バッチメソッドを使用して連絡先の 'メモ'と '名前'を更新するにはどうすればよいですか? –

+0

答えを投稿しました.. – ngesh

答えて

2
String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "= ? "; 
    String[] selectArgs2 = new String[]{contactId, "vnd.android.cursor.item/note"}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
      .withSelection(selectArgs1, selectArgs2) 
      .withValue(ContactsContract.CommonDataKinds.Note.NOTE, "[email protected]") 
      .build()); 

+0

悲しいことに、このエラーが発生しています.. "android.database.sqlite.SQLiteException:範囲外のバインドまたは列インデックス:ハンドル0x8d9ec8 06-30" –

+0

私はビットが変更されました。注...ノット...しかし、yh .. SQLエラー.. –

+0

あなたのコードのいくつかの詳細を投稿することができます..私はそれを見ることができるように.. – ngesh

関連する問題