如何向联系人添加“标签”?

时间:2021-10-31 08:35:52

I've got a fragment with a listView that displays the native contact list. The project has a feature that allows the user to add a "tag" to a contact (list item has 2 textViews, one with the name and the other one with the tag, i.e. "John Smith", "Dentist"). Any suggestion how I can add that tag? Thank you in advance.

我有一个带有listView的片段,它显示了本机联系人列表。该项目具有允许用户向联系人添加“标签”的功能(列表项具有2个textView,一个具有名称,另一个具有标签,即“John Smith”,“Dentist”)。有什么建议我可以添加该标签吗?先谢谢你。

1 个解决方案

#1


0  

Did a work around this was what I've done in the end (I'm using Notes field instead of a custom one)

我最近完成了一项工作(我使用的是Notes字段而不是自定义字段)

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

     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "=?"
                                    , new String[]{Long.toString(item.getContactId()), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE})
                            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, value)
                            .build());

                    ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                    Log.e(TAG, "The result is: " + result);
                } catch (Exception e) {
                    Log.w("UpdateContact", e.getMessage() + "");
                    for (StackTraceElement ste : e.getStackTrace()) {
                        Log.w("UpdateContact", "\t" + ste.toString());
                    }

                    Toast.makeText(context, "Update failed", Toast.LENGTH_SHORT).show();
                }

and I do a query for all the info I need

我查询了我需要的所有信息

  @Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    mSelectionArgs[0] = "1";
    return new CursorLoader(
            getActivity(),
            ContactsContract.Contacts.CONTENT_URI,
            PROJECTION,
            SELECTION,
            mSelectionArgs,
            SORT
    );
}

and to retrieve the notes

并检索笔记

   String x = "";
    String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
    String[] noteWhereParams = new String[]{id,
            ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
    Cursor noteCur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
    if (noteCur.moveToFirst()) {
        domain = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
    }
    noteCur.close();
    return x;

#1


0  

Did a work around this was what I've done in the end (I'm using Notes field instead of a custom one)

我最近完成了一项工作(我使用的是Notes字段而不是自定义字段)

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

     ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "=?"
                                    , new String[]{Long.toString(item.getContactId()), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE})
                            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, value)
                            .build());

                    ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                    Log.e(TAG, "The result is: " + result);
                } catch (Exception e) {
                    Log.w("UpdateContact", e.getMessage() + "");
                    for (StackTraceElement ste : e.getStackTrace()) {
                        Log.w("UpdateContact", "\t" + ste.toString());
                    }

                    Toast.makeText(context, "Update failed", Toast.LENGTH_SHORT).show();
                }

and I do a query for all the info I need

我查询了我需要的所有信息

  @Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    mSelectionArgs[0] = "1";
    return new CursorLoader(
            getActivity(),
            ContactsContract.Contacts.CONTENT_URI,
            PROJECTION,
            SELECTION,
            mSelectionArgs,
            SORT
    );
}

and to retrieve the notes

并检索笔记

   String x = "";
    String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
    String[] noteWhereParams = new String[]{id,
            ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
    Cursor noteCur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
    if (noteCur.moveToFirst()) {
        domain = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
    }
    noteCur.close();
    return x;

相关文章