Changing a contact's name by selecting them (change those ones which selected)

删除回忆录丶 提交于 2019-12-12 04:53:35

问题


I have this code that change all contacts names, but i want to make it to only change those ones which i selected. i mean when i touch the button, a window shows up that let me select which contacts i want to change. i mean multiple contacts, not only one:

public void editContacts() throws RemoteException, OperationApplicationException {

    int count=0;
    try {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                    builder.withSelection(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + "=?", new String[]{String.valueOf(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)))});
                    name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "haminDota2");
                    ops.add(builder.build());

                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                    count++;
                    if (count==2){
                        break;
                    }

            }

        }

        cur.close();
    } catch (Exception e) {
        Toast.makeText(this,"error",Toast.LENGTH_LONG).show();
    }

}

来源:https://stackoverflow.com/questions/45331886/changing-a-contacts-name-by-selecting-them-change-those-ones-which-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!