在获取大量的联系人 [英] Fetching a large number of contacts

查看:162
本文介绍了在获取大量的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个code在Android.by获取所有的电话号码和电子邮件。

I'm trying to fetch all the Phone numbers and Emails in Android.by using this code.

enter code here 

            String KEY_NAME = "Name";
            String KEY_NO   = "No";

    String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = 1";
    String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";


    String data="";
    String name="";
    ContactEntry contactObj;
    String id;

    List<String> temp = new ArrayList<String>();

    final String[] projection = new String[]{ContactsContract.Contacts._ID , ContactsContract.Contacts.DISPLAY_NAME , ContactsContract.Contacts.HAS_PHONE_NUMBER};

    final String[] email_projection = new String[] {ContactsContract.CommonDataKinds.Email.DATA , ContactsContract.CommonDataKinds.Email.TYPE};

    final String[] phone_projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI , projection , selection , null , sortOrder);

    if(cur.getCount()>0){

        while(cur.moveToNext()){

             id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , phone_projection , 
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);

                while (pCur.moveToNext()){

                         data =  pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if(!temp.contains(data) && !data.equals(null)){

                        }
                } 
                    pCur.close();
            }

           Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, email_projection,
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  new String[]{id}, null); 

           while (emailCur.moveToNext()){ 


                data = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                if(!temp.contains(data) && !data.equals(null)){             

                }
            } 
            emailCur.close();

        }

    }

这code是工作的罚款。但接触的大量数字让我们说5000联系人则阻止UI thread.how创建ListAdapter显示所有这些contacts.If我获取所有在后台的用户联系人会看到空的名单很长time.please提出了一些解决方案。

This code is working fine. but for the large number number of contacts let's say 5000 contacts then it blocks the UI thread.how to create a ListAdapter for displaying all these contacts.If i fetch all the contacts in background user will see the empty list for a long time.please suggest some solution

推荐答案

我甚至更低的显著多次接触过非常类似的问题,前一段时间。

I had very similar problem some time ago even with significantly lower number of contacts.

我需要填充列表视图中的所有联系人,并允许用户从中选择。最初,我是装在列表视图中的所有联系人信息。然而,这要求还真不少疑问,这是究竟是缓慢的。

I needed to populate all contacts in list view and allow the user to select from them. Initially I was loading all the contact information in the list view. However this required really a lot of queries, which is what actually is slow.

所以我改变了我的设计,我只选择了联系人姓名和联系ID和对象记录它。后来,当我的应用程序的用户选择的任何接触,我只装了他的数据。事实证明这是彻底快(如预期)。在我的情况下,它的工作完美,因为我查询了很多,我从来没有真正需要的信息(即电话号码和所有未选定的联系人的电子邮件)。

So I changed my design: I selected only the Contact name and the Contact id and recorded it in an object. Afterwards when the user of my app selected any contact I loaded only his data. This turned to be drastically faster (as expected). And in my case it worked perfectly, because I was querying a lot of information which I actually never needed (that is phone numbers and emails of all not-selected contacts).

希望你将能够重新设计你的应用程序中类似的方式。然而,如果你需要显示数据内容在ListView变量向右走,你真的可能会变成需要延迟加载列表视图带适配器(可以只希望它会即使是在快速滚动顺利执行)。

Hopefully you will be able to redesign your app in similar way. However if you need to display the contents of the data variable in the listview right away, you really might turn to need lazy-loading list view with adapter (lets just hope it will perform smoothly even on fast scroll).

这篇关于在获取大量的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆