2016-05-25 7 views
0

Ich versuche diesen Code zu verwenden, um einem Benutzer zu ermöglichen, den Namen und die Nummer eines Kontakts zu bearbeiten. Ich erhalte keine Fehler und wenn ich nameofcontact und numberofcontact in meinem Protokoll drucke, zeigt es mir die letzten Änderungen an, die ich zu dem Namen und der Nummer des Kontakts gemacht habe.versuchen, einen Kontakt mit diesem Code zu aktualisieren, funktioniert nicht

Aber es speichert nicht in meiner Kontaktdatenbank. Irgendwelche Ideen, was ist los?

public void editButton(View view) { 

//  the text in the 'nameofcontact' edittext box, can be modified by the user 
     contactname = nameofcontact.getText().toString(); 
//  the text in the 'numberofcontact' edittext box, can be modified by the user 
     contactnumber = numberofcontact.getText().toString(); 

     ContentResolver cr = getContentResolver(); 

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

     Cursor phoneCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null); 

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

//  if ((null == phoneCur) ) { 
//   createContact(name, phone); 
//  } else 
     { 
      ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
        .withSelection(where, params) 
        .withValue(ContactsContract.CommonDataKinds.Phone.DATA, contactnumber) 
        .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(); 
     } 

     System.out.println (contactname); 
     System.out.println (contactnumber); 
     Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show(); 


    } 

Antwort

0

Bitte überprüfen Sie unten Erlaubnis in Ihrem manifeast hinzufügen oder nicht

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 

und Sie diese Methode verwenden, kann es für mich funktioniert.

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

    try { 
     name = name.trim(); 

     number = number.trim(); 

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

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

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

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

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

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

       ops.add(ContentProviderOperation 
         .newUpdate(
           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; 
} 
+0

Ja, haben Sie das, danke. Ich habe andere Methoden zum Löschen und Hinzufügen und sie funktionieren gut. – CHarris