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

时间:2014-06-17 17:26:30

标签: android contacts

我的自动完成联系建议有什么问题,完成加载过程需要3-4秒。我的手机里有大约200个联系人。该活动用于撰写消息,其中用户键入/搜索联系人并写入要发送给收件人的消息。

在我的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();

加载联系人的方法:

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();

    }

修改

感谢Matiash。现在这是我的工作方法,与上述相比,速度非常快。

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();
}

1 个答案:

答案 0 :(得分:2)

您正在进行嵌套循环,这意味着n次查询(与您有联系人一样多)。

由于显然您对所有联系人的电话号码感兴趣,我建议仅在ContactsContract.CommonDataKinds.Phone.CONTENT_URI内容提供商处进行迭代(不使用联系人ID的过滤器)。您从Contacts读取的字段也会出现在该提供商中。

例如:

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);
    ...

这应该有更好的表现。