取联系细节花费大量时间在android系统? [英] Fetching Contact detail Take A lot of time in android?

查看:111
本文介绍了取联系细节花费大量时间在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我目前在做联系,如果我从接触* (电子邮件,电话号码和联系人姓名)的* 并确实well.But获取详细信息相关项目问题是,它需要很长的时间来获取联系方式(1000+接触包括联系,从社交网站同步)是.so,我把一个异步任务为此,它也很好,但问题是,由于很长一段时间才能完成取过程中,当我preSS后退按钮异步任务期间,它崩溃。我的问题不会崩溃为什么取接触采取了很多time.Is有办法让联系人更快。我的code得到的是如下

给出的联络方式

 公共无效readContact(){
    CONTACTNAME =新的ArrayList<串GT;();
    contactnumber =新的ArrayList<串GT;();
    companyname_one =新的ArrayList<串GT;();
    contactemail =新的ArrayList<串GT;();
    人= getContentResolver()查询(
            ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,
            PhoneLookup.DISPLAY_NAME);    而(people.moveToNext()){
        INT nameFieldColumnIndex =人
                .getColumnIndex(PhoneLookup.DISPLAY_NAME);
        弦接触= people.getString(nameFieldColumnIndex);
        如果(接触== NULL){
            contactname.add(无触点集);
        }其他{            contactname.add(接触);        }        字符串szId = people.getString(人
                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));        cursor_one = getContentResolver()查询。(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                空值,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +='
                        + szId +',NULL,NULL);
        如果(cursor_one.moveToNext()){
            串号= cursor_one.getString(cursor_one
                    .getColumnIndex(Phone.NUMBER));
            contactnumber.add(数);
            cursor_one.close();        }其他{
            contactnumber.add(无编号);
            cursor_one.close();        }        emails_value = getContentResolver()查询。(
                Email.CONTENT_URI,
                空值,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +='
                        + szId +',NULL,NULL);        如果(emails_value.moveToNext()){
            email_sorting = emails_value.getString(emails_value
                    .getColumnIndex(Email.DATA));
            选择所有();        }其他{            contactemail.add(无电子邮件);
            emails_value.close();        }    }    people.close();
    的System.out.println(NOZ+ contactnumber);
    的System.out.println(名+联系人姓名);
    的System.out.println(电子邮件+ contactemail);
    的System.out.println(NOZ大小+ contactnumber.size());
    的System.out.println(名大小+ contactname.size());
    的System.out.println(contactemail大小+ contactemail.size());}

checkAll()方法是电子邮件的行话匹配如下

 公共布尔checkAll(){    布尔chkAll =真;
    图案P1 = Pattern.compile(+ @ + \\\\ [A-Z] +。。。);
    匹配器M1 = p1.matcher(email_sorting.trim());    如果(!m1.matches()){        contactemail.add(无电子邮件);
        contactemail_sort.add(无电子邮件);
        emails_value.close();
        chkAll = FALSE;
    }其他{        contactemail.add(email_sorting);
        contactemail_sort.add(email_sorting);
        emails_value.close();
        chkAll = TRUE;
    }    返回chkAll;
}


解决方案

好吧,我想,我终于看到了这个工作的最佳途径。不要扯所有的联系人信息了您创建适配器之前,您应该创建一个接受你的光标并填充你的<$ C $自定义视图自定义的CursorAdapter C>是在后台线程中执行cursor_one 查询。这应该采取的ListView的天然惰性加载的优势,像你想使这项工作。

如果您使用至少Android 3.0的,你可以用装载机而不使用的AsyncTask的。

我发现了一个自定义光标适配器这里的一个例子这是我用来做我的榜样。有在code来实现这个可能是更好的方法,但是这至少说明你覆盖的方法和要放什么东西在其中。

 公共类ContactListCursorAdapter扩展SimpleCursorAdapter {    私人上下文的背景下;    私人诠释布局;    公共ContactListCursorAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []到){
        超(背景下,布局,C,从,到);
        this.context =背景;
        this.layout =布局;
    }    //用于填充新的列表项
    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){        光标C = getCursor();        最后LayoutInflater吹气= LayoutInflater.from(背景);
        视图V = inflater.inflate(布局,父母,假);        INT nameCol = c.getColumnIndex(People.NAME);        字符串名称= c.getString(nameCol);        / **
         *下一组的条目的名称。
         * /
        TextView的name_text =(TextView中)v.findViewById(R.id.name_entry);
        如果(name_text!= NULL){
            name_text.setText(名);
        }        getDeatils(V,C);        返回伏;
    }    //用于从适配器一个新项目绑定到现有视图
    @覆盖
    公共无效bindView(视图V,上下文的背景下,光标C){        INT nameCol = c.getColumnIndex(People.NAME);        字符串名称= c.getString(nameCol);        / **
         *下一组的条目的名称。
         * /
        TextView的name_text =(TextView中)v.findViewById(R.id.name_entry);
        如果(name_text!= NULL){
            name_text.setText(名);
        }        getDetails(V,C);
    }    私人无效populateDetails(视图V,光标C){
       //开始您的AsyncTask或加载器来获取详细信息。
       //请务必在结果填充视图
       //适当的完成回调。
    }
}

Hi am currently doing a project related to Contact ,Where I am fetching details from the contact*(Email,Number and Contactname)* and it does well.But the problem is it take a long time to fetch the contact details ( 1000+ contact including contact that sync from social network sites).so that I put a Asynchronous Task for this purpose ,and it does well but the problem is due to a long time to complete the fetching process ,when I press the back button it crash during the Asynchronous task .My problem does not crash why this fetching contact take a lot of time.Is there way to get the contact faster. My code to get contact detail is as given below

public void readContact() {
    contactname = new ArrayList<String>();
    contactnumber = new ArrayList<String>();
    companyname_one = new ArrayList<String>();
    contactemail = new ArrayList<String>();
    people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null,
            PhoneLookup.DISPLAY_NAME);

    while (people.moveToNext()) {
        int nameFieldColumnIndex = people
                .getColumnIndex(PhoneLookup.DISPLAY_NAME);
        String contact = people.getString(nameFieldColumnIndex);
        if (contact == null) {
            contactname.add("No contact Set");
        } else {

            contactname.add(contact);

        }

        String szId = people.getString(people
                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

        cursor_one = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
                        + szId + "'", null, null);
        if (cursor_one.moveToNext()) {
            String number = cursor_one.getString(cursor_one
                    .getColumnIndex(Phone.NUMBER));
            contactnumber.add(number);
            cursor_one.close();

        } else {
            contactnumber.add("no number");
            cursor_one.close();

        }

        emails_value = getContentResolver().query(
                Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
                        + szId + "'", null, null);

        if (emails_value.moveToNext()) {
            email_sorting = emails_value.getString(emails_value
                    .getColumnIndex(Email.DATA));
            checkAll();

        } else {

            contactemail.add("no email");
            emails_value.close();

        }

    }

    people.close();


    System.out.println("noz " + contactnumber);
    System.out.println("name" + contactname);
    System.out.println("email" + contactemail);
    System.out.println("noz size " + contactnumber.size());
    System.out.println("name size " + contactname.size());
    System.out.println("contactemail size " + contactemail.size());



}

The checkAll() method is patter matching of email as below

    public boolean checkAll() {

    boolean chkAll = true;
    Pattern p1 = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher m1 = p1.matcher(email_sorting.trim());

    if (!m1.matches()) {

        contactemail.add("no email");
        contactemail_sort.add("no email");
        emails_value.close();
        chkAll = false;


    } else {

        contactemail.add(email_sorting);
        contactemail_sort.add(email_sorting);
        emails_value.close();
        chkAll = true;
    }

    return chkAll;
}

解决方案

Ok, I think I finally see the best way for this to work. Instead of pulling all of the contact information out before you create your adapter, you should create a custom CursorAdapter that accepts your people cursor and populates a custom view with your cursor_one query that is executed on a background thread. This should take advantage of the natural lazyloading of the ListView and make this work like you want.

If your using at least Android 3.0, you can use Loaders instead of using an AsyncTask.

I found an example of a custom cursor adapter here which is what I used to make my example. There are probably better ways to implement this in code, but this at least shows you which methods to override and what to put in them.

public class ContactListCursorAdapter extends SimpleCursorAdapter {

    private Context context;

    private int layout;

    public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.layout = layout;
    }

    // Used to populate new list items
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }

        getDeatils(v,c);

        return v;
    }

    // Used to bind a new item from the adapter to an existing view
    @Override
    public void bindView(View v, Context context, Cursor c) {

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }

        getDetails(v,c);
    }

    private void populateDetails(View v, Cursor c) {
       // Start your AsyncTask or Loader to get the details.
       // Be sure to populate the view with the results in the
       // appropriate completion callback.
    }
}

这篇关于取联系细节花费大量时间在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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