15

我正在实现一个 AutoCompleteTextView,我需要我所有联系人的姓名和电子邮件。我发现我正在异步运行这个片段,但它非常慢。

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

if (cur.getCount() > 0) {               
    while (cur.moveToNext()) {                  
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));                   
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); 

            while (emailCur.moveToNext()) { 

                String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    autoCompleteAdapter.add(name + " - " + email);
            }

            emailCur.close();
        }
    }
}

我正在执行一种内部查询,我认为这就是问题所在。有没有办法调整它并使其更快?

4

3 回答 3

56
private static final String[] PROJECTION = new String[] {
    ContactsContract.CommonDataKinds.Email.CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Email.DATA
};

...

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
    try {
        final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
        final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
        long contactId;
        String displayName, address;
        while (cursor.moveToNext()) {
            contactId = cursor.getLong(contactIdIndex);
            displayName = cursor.getString(displayNameIndex);
            address = cursor.getString(emailIndex);
            ...
        }
    } finally {
        cursor.close();
    }
}

几点注意事项:

  • ContactsContract.CommonDataKinds.Email.CONTENT_URI用于获取您需要的信息,请参阅ContactsContract.CommonDataKinds.Email了解您可以查询哪些列
  • 使用投影仅获取您真正需要的那些列,您可以节省一些内存并提高查询性能
  • 仅在 while 循环之前获取一次列索引
于 2012-08-25T11:42:10.527 回答
6

您不应该直接查询ContactsContract.Contacts

使用电子邮件数据类型对ContactsContract.CommonDataKinds进行一次查询。

ContactsContract.CommonDataKinds.Email继承了许多可用于构建投影的其他接口。(请参阅文档中的继承常量)

例如 :

import android.provider.ContactsContract.CommonDataKinds.Email;

[...]

public static final String[]  EMAILS_PROJECTION = new String[] {
    Email._ID,
    Email.DISPLAY_NAME_PRIMARY,
    Email.ADDRESS
};

Email.CONTENT_URI

您可以直接从电子邮件数据类型中检索大量信息(例如用户 ID、用户显示名称...)。

编辑:

我刚刚意识到您正在尝试构建一个 AutoCompleteTextView。

您应该覆盖CursorAdapter的runQueryOnBackgroundThread方法和方法并使用Email.CONTENT_FILTER_URIconvertToString

真的强烈建议你看看ApiDemo 示例

尤其是您可以在此处找到的AutoComplete4.java示例。

于 2012-08-24T13:02:38.340 回答
0
ContentResolver cr = mContext.getContentResolver(); 
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, "HAS_PHONE_NUMBER <> 0", null, null); 

if (cursor!= null) 
{ 
    final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
    final int numberIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); 
    final int idIndex= cursor.getColumnIndex(ContactsContract.Contacts._ID); 
    String displayName, number = null, idValue; 

    while (cursor.moveToNext()) 
    {
        displayName = cursor.getString(displayNameIndex);
        idValue= cursor.getString(idIndex);

        Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = '" + idValue + "'", null, null);
        phones.moveToFirst();

        try 
        {
            number = phones.getString(phones.getColumnIndex("data1"));
        }
        catch (CursorIndexOutOfBoundsException e)
        {
        }

        phones.close();
        userList.add(new ContactModel(displayName, number, null));
    }
}
于 2016-07-31T09:34:04.837 回答