2017-09-06 5 views
3

매우 이상한 꼴입니다. 이 규칙에 따라 연락처 이름을 업데이트하려고합니다 : - 연락처의 이름이 "bit"+ space ("bit")로 시작하면 연락처의 이름을 name.substring (4, name.length())로 업데이트하십시오. 이는 연락처 이름이 "비트"없이 업데이트된다는 것을 의미합니다.연락처 이름에 곱하기 업데이트시 (ContentProviderOperation)

나는 4를 낮추는 번호에서 name.substring을 사용할 때 (나는 연락처의 이름에 공백이 올 때까지 생각한다.) 완벽하게 작동한다. 4 자 이상부터 사용할 때 연락처의 이름이 늘어납니다. 예를 들어, 나는 name = name.substring (4, name.length())을 사용할 때 "bit Lili"와 같은 이름을 사용합니다. Lili Lili.

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();}} 

고마워요!

답변

0

아니 어떤 대답하지만 당신이 가지고있는 문제를 작업 할 생각은

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

그래서 내 수정 제안은 가족의 이름으로 변경하고 지정된 이름이 변경되는 당신의 질문에 따라 필요로 함께 지정된 이름을 제거하여 해당 수정 사항을 수정하려고합니다.

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; 
} 
관련 문제