Android 仅在列出了电子邮件的联系人中获取光标 >android 2.0

发布于 2024-10-20 17:52:21 字数 953 浏览 5 评论 0原文

我有以下代码来从内容提供商获取联系人

String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.PHOTO_ID };
        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                columns, null, null, null);

,并使用此代码通过 ID 获取特定联系人的电子邮件:

Cursor emails = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contact.getContactId(), null, null);

我当前的实现传递光标中的每一行并获取其电子邮件并存储它们位于 java 对象的 arrayList 中。

我想知道是否可以做的只是查询内容提供商并返回包含 ids/name 等联系人的光标,其中列出了电子邮件地址。

这种方式获取联系人列表的等待时间较长。我正在使用此列表作为列表适配器。如果我只能获取有电子邮件的联系人,我可以在列表中使用光标适配器。

这样的事情可能吗?我怎样才能加快这个过程?

i have the following code to get contacts from content provider

String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.PHOTO_ID };
        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                columns, null, null, null);

and i use this one to get the emails for a specific contact by their id:

Cursor emails = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contact.getContactId(), null, null);

my current implementation passes every row in the cursor and aquires its emails and stores them in an arrayList of java objects.

what i was wondering if it was possible to do is just query the content provider and return a cursor of just contacts with ids/name etc that have an email address listed.

this way has a long waiting period for getting the contact list. i am using this list for a list adapter. if i can get only the contacts that have an email i can use a cursor adapter in my list.

Is something like this possible? how can i speed up the process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-10-27 17:52:21

@CapDroid

修复了 DArkO 帖子中的工作代码:

    ContentResolver cr = context.getContentResolver();
    String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID,
            ContactsContract.CommonDataKinds.Email.DATA, 
            ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String order = "CASE WHEN " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + ", " 
            + ContactsContract.CommonDataKinds.Email.DATA
            + " COLLATE NOCASE";
    String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);

您的光标将包含基本 ID 以及姓名和电子邮件地址。该代码的性能非常好,因为它只请求很少的列。

@CapDroid

Fixed working code from DArkO's post:

    ContentResolver cr = context.getContentResolver();
    String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID,
            ContactsContract.CommonDataKinds.Email.DATA, 
            ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String order = "CASE WHEN " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + ", " 
            + ContactsContract.CommonDataKinds.Email.DATA
            + " COLLATE NOCASE";
    String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);

Your cursor will have essential IDs as well as names and email addresses. The performance of this code is great because it requests few columns only.

甜心小果奶 2024-10-27 17:52:21

我解决了这个问题,这是如何完成的:

更新

   String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
        Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

    String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
        + " NOT LIKE '%@%' THEN 1" + " ELSE 2 END, "
        + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
    String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;

    return mContent.query(Email.CONTENT_URI,
                      PROJECTION, filter, null, order);

I solved this one, here is how its done:

UPDATE

   String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
        Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

    String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
        + " NOT LIKE '%@%' THEN 1" + " ELSE 2 END, "
        + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
    String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;

    return mContent.query(Email.CONTENT_URI,
                      PROJECTION, filter, null, order);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文