Arpit Singh
Arpit Singh

Reputation: 37

How to fetch all contact quickly using android application?

I am trying to fetch all contact of mobile phone and it's fetching but very slow. I am trying to fetch quickly. It's taking time because i have used loop. I want to fetch only Contact Name and Phone Number.

private void getAllContacts() {
        ArrayList<ContactlistModel> contactVOList = new ArrayList();
        ContactlistModel contactVO;
        ContentResolver contentResolver = getActivity().getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    contactVO = new ContactlistModel();
                    contactVO.setContact_name(name);

                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id},
                            null);
                    if (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contactVO.setPhone_no(phoneNumber);
                    }
                    phoneCursor.close();
                    Cursor emailCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (emailCursor.moveToNext()) {
                        String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    }
                    contactVOList.add(contactVO);
                    progressDialog.dismiss();
                }
            }
            contactAdapter = new ContactAdapter(getContext(),contactVOList, listener);
            list.setLayoutManager(new LinearLayoutManager(getActivity()));
            list.setAdapter(contactAdapter);
        }
    }

Upvotes: 0

Views: 74

Answers (2)

Mohammad didehvar
Mohammad didehvar

Reputation: 90

I found the below code from another question and it works for me.

   ArrayList<Contact> contacts = new ArrayList<Contact>();

    Cursor c = getActivity().getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "asc");
    while (c.moveToNext()) {

        String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Contact ct = new Contact();
        ct.setName(contactName);
        ct.setNumber(phNumber);

        contacts.add(ct);

    }
    c.close();

Upvotes: 4

aryanknp
aryanknp

Reputation: 1167

Replace null by passing data you require, in a String array like this:

Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,  new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

Then while you loop over your cursor:

String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

 String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

Upvotes: 0

Related Questions