2017-09-06 3 views
3

Un très étrange problème. Je suis à mettre à jour le nom de contacts par cette règle: - si le nom d'un contact commence par "bit" + espace ("bit") alors -> mettre à jour le nom du contact à name.substring (4, name.length()) , et cela signifie que le nom du contact sera mis à jour sans le "bit". Quand j'utilise name.substring à partir du numéro qui les abaisse 4 (je pense jusqu'à l'espace au nom du contact) son fonctionnement est parfait. Quand j'utilise à partir du 4 caractères le nom du contact se multiplie. Par exmaple, quand j'utilise name = name.substring (4, name.length()) alors que le nom est égal à "bit Lili" sa mise à jour à: Lili Lili.Multipliez le nom des contacts Lors de la mise à jour (ContentProviderOperation)

private void updateContact(String name) { 
    ContentResolver cr = getContentResolver(); 
    String where = ContactsContract.Data.DISPLAY_NAME + " = ?"; 
    String[] params = new String[] {name}; 
    Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI,null,where,params,null); 
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
    if ((null == phoneCur)) {//createContact(name, phone); 
     Toast.makeText(this, "no contact with this name", Toast.LENGTH_SHORT).show(); 
     return;} else {ops.add(ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI) 
       .withSelection(where, params) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name.substring(4,name.length())) 
       .build()); 
    } 

    phoneCur.close(); 

    try {cr.applyBatch(ContactsContract.AUTHORITY, ops);} 
    catch (RemoteException e) {e.printStackTrace();} 
    catch (OperationApplicationException e) {e.printStackTrace();}} 

Merci!

Répondre

0

Pas une certaine réponse, mais il suppose de travailler la question que vous avez est le

.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME //This specific part has a problem with the new update function 
      ,name.substring(4,name.length())) 

donc ma proposition de solution est de changer pour le nom de famille et prénom modifier ceux-ci que vous avez besoin en fonction de votre question vous voulez supprimer le prénom, donc c'est un correctif pour cela

public static boolean updateContactName(@NonNull Context context, @NonNull String name) { 
    if (name.length() < 4) return true; 
    String givenNameKey = ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME; 
    String familyNameKey = ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME; 
    String changedName = name.substring(4, name.length()); 
    ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 

    String where = ContactsContract.Data.DISPLAY_NAME + " = ?"; 
    String[] params = new String[]{name}; 

    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) 
      .withSelection(where, params) 
      .withValue(givenNameKey, changedName) 
      .withValue(familyNameKey, "") 
      .build()); 
    try { 
     context.getContentResolver() 
       .applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
}