【问题标题】:Multiply contacts' name On update (ContentProviderOperation)将联系人姓名乘以更新(ContentProviderOperation)
【发布时间】:2018-02-15 10:10:21
【问题描述】:

一个非常奇怪的问题。我正在尝试通过此规则更新联系人姓名: - 如果联系人姓名以“bit”+空格(“bit”)开头,那么 -> 将联系人姓名更新为 name.substring(4, name.length()),这意味着联系人姓名将在没有“”的情况下更新少量 ”。

当我使用将它们降低 4 的数字中的 name.substring 时(我认为直到联系人姓名中的空格)它工作得很好。当我从 4 个字符开始使用时,联系人的姓名会相乘。例如,当我使用 name = name.substring(4, name.length()) 而 name 等于“bit 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();}}

谢谢!

【问题讨论】:

    标签: java android list contact android-contentresolver


    【解决方案1】:

    不是一个确定的答案,但它应该可以解决您遇到的问题

    .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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 2012-10-11
      • 2020-08-09
      • 2016-05-07
      • 2017-06-16
      • 2013-09-07
      相关资源
      最近更新 更多