尝试加载联系人列表时出现性能问题。这是代码:
public ContactList getContacts(String constraint)
{
ContactList contactList = new ContactList();
Uri uri = ContactsContract.Contacts.CONTENT_URI;
if (constraint != null && constraint.equals("") == false)
{
uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI, constraint);
}
ContentResolver cr = _context.getContentResolver();
String sortOrder = DISPLAY_NAME + " COLLATE LOCALIZED ASC";
// Cursor cur = cr.query(uri, null, null, null, sortOrder);
String[] arrayOfString = new String[1];
arrayOfString[0] = "1";
Cursor cur = cr.query(uri, null, "has_phone_number=?", arrayOfString,
sortOrder);
if (cur.getCount() > 0)
{
setBaseContactList(contactList, cur);
}
cur.close();
return contactList;
}
private void setBaseContactList(ContactList contactList, Cursor cur)
{
String id;
String name;
String lookUpKey;
String photoId;
while (cur.moveToNext())
{
id = cur.getString(cur.getColumnIndex(DISPLAY_ID));
name = cur.getString(cur.getColumnIndex(DISPLAY_NAME));
photoId = cur.getString(cur.getColumnIndex(PHOTO_ID));
lookUpKey = cur.getString(cur.getColumnIndex(LOOKUP_KEY));
Contact c = new Contact(id, name, photoId, lookUpKey);
contactList.addContact(c);
}
}
查看traceview我可以看到 cur.getString(cur.getColumnIndex("Some column name")); 是否会影响加载时间
使用traceview时我可以看到的另一件事是该方法有很多调用
String.equalsIgnoreCase()
我的猜测是 cur.getString 正在调用它。
我正在考虑将所有在onCreate()中启动的值放在一个HT(ContactID, Contact)中,然后在每次从联系人数据库中选择之后使用提取的用户 ID 调用它们。我将减少使用量 3 次,这很好,但有更好的解决方案吗?
谢谢