开发者

Android - How to fetch a contact name and number without using Cursors?

开发者 https://www.devze.com 2023-03-08 19:18 出处:网络
I have a big performance issue in my app. After going through traceview i found that most of my app\'s performance has been consumed by cursors. So i was wondering is there any alternative to Cursors

I have a big performance issue in my app. After going through traceview i found that most of my app's performance has been consumed by cursors. So i was wondering is there any alternative to Cursors for dealing with device contact list. And if there is no alternative then please advise me how to deal with cursors so it won't slow down your app.

Please HELP!!

Thanks.

This part of my code has performance issue :-

public void getDisplayName()
{       
    Cursor c1 = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    String personName = null, number = null;
    try 
    {
        Log.e(TAG, "I am here!!");
        if(c1.getCount() > 0)
        {
            Log.e(TAG, "I am here2!!");
            while(c1.moveToNext())
            {
                HashMap<String,String> item = new HashMap<String,String>();
                String id = c1.getString(c1.getColumnIndex(Contacts._ID));
                personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
                item.put("Name", personName);
                Cursor cur = this.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null)开发者_StackOverflow中文版;
                while(cur.moveToNext())
                {
                    Log.e(TAG, "I am here!!3");
                    number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                    item.put("Number", number);
                }
                displayName.add(item);          
            }
        }
    }
    finally
    {
        c1.close();
    }
}


How to fetch a contact name and number without using Cursors?

That is not possible.

I have a big performance issue in my app.

Rather than using a single query, you use N+1 queries, where N is the number of rows returned by the other query. This is guaranteed to give you poor performance compared to just doing a single query.

You can get the user's name and _ID along with the phone numbers:

    String[] PROJECTION=new String[] { Contacts._ID,
                                        Contacts.DISPLAY_NAME,
                                        Phone.NUMBER
                                        };
    Cursor c=a.managedQuery(Phone.CONTENT_URI, PROJECTION, null, null, null);

Also, never call getColumnIndex() in a loop, since the value never changes.


I was working on same code and I need email and phone numbers both in first approach It took around 35 seconds to fetch 612 contacts, when I optimized it, it took 2 seconds to fetch 612 contacts

my code.

ContentResolver cr = context.getContentResolver();

    String[] projection = new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER };

    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
    String[] selectionArgs = { String.valueOf(1) };

    Cursor cur = cr.query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String phNo = cur.getString(cur.getColumnIndex(Phone.NUMBER));
            ContactModel contactModel = new ContactModel();
            contactModel.setName(name);
            contactModel.setPhoneNumber(phNo);
            contactList.add(contactModel);
        }
    }

    String[] projectionEmail = new String[] { Contacts.DISPLAY_NAME, Email.ADDRESS };

    String selectionEmail = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
    String[] selectionArgsEmail = { String.valueOf(1) };

    Cursor curEmail = cr.query(Email.CONTENT_URI, projectionEmail, selectionEmail, selectionArgsEmail, null);

    if (curEmail.getCount() > 0) {
        while (curEmail.moveToNext()) {
            String Emailname = curEmail.getString(curEmail.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String email = curEmail.getString(curEmail.getColumnIndex(Email.ADDRESS));
            ContactModel contactModel = new ContactModel();
            contactModel.setName(Emailname);
            contactModel.setEmailId(email);
            contactList.add(contactModel);
        }
    }

`

0

精彩评论

暂无评论...
验证码 换一张
取 消