【问题标题】:AutoCompleteTextView taking so long to reload contactsAutoCompleteTextView 需要很长时间才能重新加载联系人
【发布时间】:2014-07-11 12:07:53
【问题描述】:

我的自动完成联系建议有什么问题,需要 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();

    }

编辑

感谢马蒂亚什。这是我现在的工作方法,与上面相比它非常快..

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

【问题讨论】:

  • 用户输入字母时速度很慢并且应该出现结果的小部件?发布您拥有的适配器。
  • 我在上面编辑了我的工作代码...问题现在解决了..谢谢!
  • 感谢更新人!拯救我的一天

标签: android contacts


【解决方案1】:

您正在执行一个嵌套循环,这意味着 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);
    ...

这应该有更好的性能。

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多