English 中文(简体)
Android: 添加吸引联系人作为最喜爱的联系人吗?
原标题:Android: Adding exsting contacts as favourite contact?

我有一个 ListView 列表, 其中我正在获取所有手机联系人 。 在 ListView 中, i 已放置一个按钮, 并点击此按钮, 联系人应该添加到最喜爱的选项中, 然后再点击此按钮, 并且应该从最喜爱的选项中删除 。 < br/ > 这里我找到了代码, 以便添加与最喜爱标签的新联系人 。 但是在点击 listView 中, 如何做到这一点 。 i 已尝试过此代码 :

<强度 > 守则:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();

        ops.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null)
                .withValue(RawContacts.STARRED, true).build());
        ops.add(ContentProviderOperation
                .newUpdate(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
                        rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, "91-0000000002").build());
        ops.add(ContentProviderOperation
                .newUpdate(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID,
                        rawContactInsertIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, "Maulik patel").build());

Using this code all contacts are added to the favourite
thanks

最佳回答

在这里,我终于找到了我最喜欢的具体接触方式。

ContentValues values = new ContentValues();
        String[] fv = new String[] { "juned" };
        values.put(Contacts.STARRED, 1);
        getContentResolver().update(Contacts.CONTENT_URI, values,
                Contacts.DISPLAY_NAME + "= ?", fv);

仅将不喜爱的 1 改为 0 。

问题回答

我需要添加/删除到最喜爱的联系人, 使用特定的联系人 ID 而不是联系人名, 所以我已经修改了 juds 代码, 此处是 :

int contactId = 10;    // some contact s ID

ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.STARRED, 1);
getActivity().getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, values,
    ContactsContract.Contacts._ID + "= ?", new String[] { String.valueOf(contactId) });

区别在于,您只需将 DISPLAY_NAME 改为





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签