【问题标题】:Custom ListView for displaying Contacts takes more time to load用于显示联系人的自定义 ListView 需要更多时间来加载
【发布时间】:2012-04-26 11:20:41
【问题描述】:

在我的应用程序中,启动时我想显示所有带有搜索过滤器选项的联系人。我已经实现了这一点,但问题是 - 加载联系人时,需要更多时间。如何提高代码的效率?下面的代码 sn-p 显示了我如何检索联系人:

public ContactList newContactList(Context ctx) {

ContactList contacts = new ContactList();

String id = "";

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

try {

    this.cur = this.cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);       
    if (this.cur.getCount() > 0) {
        while (cur.moveToNext()) {
            Contact c = new Contact();
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                c.setId(id);
                c.setDisplayName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                c.setPhone(this.getPhoneNumbers(ctx, id));
                c.setEmail(this.getEmailAddresses(id));
                c.setNotes(this.getContactNotes(id));
                c.setAddresses(this.getContactAddresses(id));
                c.setImAddresses(this.getIM(id));
                c.setOrganization(this.getContactOrg(id));
                //c.setPicture(loadContactPhoto(cr, cur.getLong(cur.getColumnIndex(CommonDataKinds.Photo.CONTACT_ID))));
                contacts.addContact(c);
            }               
        }
    }
    }
    catch(Exception e)
    {
        throw new IllegalStateException(e);
    }
    finally
    {
        cur.close();            
    }

    return(contacts);
}

ContactList 是一个返回所有联系人的 ArrayList 的类,我使用这个 ArrayList 将它设置为我的自定义适配器类。我想,因为通过游标迭代来获取数组列表需要很多时间。但是,我需要这个数组列表来根据搜索条件过滤联系人并显示自定义联系人列表。有什么办法可以提高代码的性能?

【问题讨论】:

    标签: android android-contacts custom-lists custom-adapter performance


    【解决方案1】:

    【讨论】:

    • 感谢 Alexandre 的快速回复。我尝试使用SimpleCursorAdapter 显示联系人,它正在快速加载联系人。但是你能指导我如何在联系人列表中的任何项目上实现 onClick 方法,该方法应该采用 ID 并相应地显示特定的联系人详细信息。下面是我在 ListActivity 中用来显示联系人的代码。
    • 在您的 ListView 上,只需添加 setOnItemClickListener mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }); 并在 onItemClick 方法中,使用您的自定义 adatper 获取类似 mListView.getAdapter().getItem(arg2); 的项目
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2019-11-08
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多