简体   繁体   English

AutoCompleteTextView需要很长时间才能重新加载联系人

[英]AutoCompleteTextView taking so long to reload contacts

What is wrong with my auto complete contact suggestion it takes 3-4 seconds to complete the loading process. 我的自动完成联系建议有什么问题,它需要3-4秒才能完成加载过程。 I have around 200 contacts in my phone. 我的手机中大约有200个联系人。 The activity is for compose message where the user to type/search contacts and write message to send to the recipient. 该活动用于撰写消息,用户可以在其中键入/搜索联系人并编写消息以发送给收件人。

Inside my oncreate method: 在我的oncreate方法中:

mPeopleList = new ArrayList<Map<String, String>>();
    SimpleAdapter mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcoview,new String[] { "Name", "Phone", "Type" }, new int[] {R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    textView.setThreshold(1);
    textView.setAdapter(mAdapter);
PopulatePeopleList();

method to load contacts: 加载联系人的方法:

public void PopulatePeopleList(){
        int i =0;

        Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (people.moveToNext()){
            String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if ((Integer.parseInt(hasPhone) > 0)){
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                null, null);

                while (phones.moveToNext()){
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    if(numberType.equals("0"))
                        NamePhoneType.put("Type", "Work");
                    else
                        if(numberType.equals("1"))
                            NamePhoneType.put("Type", "Home");
                        else if(numberType.equals("2"))
                            NamePhoneType.put("Type", "Mobile");
                        else
                            NamePhoneType.put("Type", "Other");
                    mPeopleList.add(NamePhoneType); //add this map to the list. 
                }
                phones.close();
            }else continue;
        }
        people.close();

    }

EDIT 编辑

Thanks for Matiash. 感谢Matiash。 This is now my working method and it is very fast compared to above.. 现在这是我的工作方法,与上面的方法相比,速度非常快。

public void readContacts(){
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    int colDisplayName = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int colPhoneNumber = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int colPhoneType = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);

    while (phones.moveToNext()) {
        String contactName = phones.getString(colDisplayName);
        String phoneNumber = phones.getString(colPhoneNumber);
        String numberType = phones.getString(colPhoneType);


        Map<String, String> NamePhoneType = new HashMap<String, String>();
        NamePhoneType.put("Name", contactName);
        NamePhoneType.put("Phone", phoneNumber);
        if(numberType.equals("0"))
            NamePhoneType.put("Type", "Work");
        else
            if(numberType.equals("1"))
                NamePhoneType.put("Type", "Home");
            else if(numberType.equals("2"))
                NamePhoneType.put("Type", "Mobile");
            else
                NamePhoneType.put("Type", "Other");
        mPeopleList.add(NamePhoneType); //add this map to the list. 
    }phones.close();
}

You're doing a nested loop, which means n queries (as many as you have contacts). 您正在执行嵌套循环,这意味着n个查询(与您的联系人一样多)。

Since apparently you're interested in the phone numbers of all contacts, I would suggest iterating only on the ContactsContract.CommonDataKinds.Phone.CONTENT_URI content provider (without a filter for contact id). 由于您显然对所有联系人的电话号码都感兴趣,因此建议仅在ContactsContract.CommonDataKinds.Phone.CONTENT_URI内容提供程序上进行迭代(不使用联系人ID筛选器)。 The fields you're reading from Contacts are also present in that provider. 您从“ Contacts中读取的字段也显示在该提供程序中。

For example: 例如:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

colDisplayName = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int colPhoneNumber = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int colPhoneType = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);

while (phones.moveToNext()) {
    String contactName = phones.getString(colDisplayName);
    String phoneNumber = phones.getString(colPhoneNumber);
    String numberType = phones.getString(colPhoneType);
    ...

This should have much better performance. 这应该具有更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM