1

I want to fetch 50 by 50 contacts from the contact list.

I tried for loop but it gives me 10 contacts randomly.

How to fetch 50 by 50 contacts .. please help me

My code :

ArrayList<Contact_Model> contactList = new ArrayList<Contact_Model>();
            Uri uri = ContactsContract.Contacts.CONTENT_URI;

            Cursor contactsCursor = getContentResolver().query(uri, null, null,
                    null, ContactsContract.Contacts.DISPLAY_NAME + " ASC "); // Return

            if (contactsCursor.moveToFirst()) {

                do {

                    long contctId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
                    Uri dataUri = ContactsContract.Data.CONTENT_URI; // URI to get
                    Cursor dataCursor = getContentResolver().query(dataUri, null,
                            ContactsContract.Data.CONTACT_ID + " = " + contctId,
                            null, null);

                    // Strings to get all details
                    String displayName = "";
                    String mobilePhone = "";
                    String contactNumbers = "";
                    String cNumber = "";
                    String contactImage = "";

                    String imnage = "";
                    Cursor phonesCursor = null;
                    Person.Urls urls;

                    try {
                        Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("Phone number"));
                        phonesCursor = context.getContentResolver().query(phoneUri, new String[]{ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI}, null, null, null);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }

                    if (dataCursor.moveToFirst()) {
                        try {
                            imnage = dataCursor.getString(1);
                            Log.e("detail", "==============" + imnage);
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }
                    }

                    if (dataCursor.moveToFirst()) {

                        displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));// get

                        do {
                            for (i = 0; i < 50; i++) {
                                if (dataCursor. getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {

                                    switch (dataCursor.getInt(dataCursor.getColumnIndex("data2"))) {

                                        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                            break;
                                        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                            break;
                                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                            contactNumbers = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                            contactNumbers += mobilePhone;
                                            int id = 1;

                                            if (String.valueOf(contactNumbers).charAt(0) == '+') {
                                                if (contactNumbers.length() == 13) {
                                                    String trim_num = contactNumbers.substring(3);
                                                    cNumber = trim_num;
                                                    cNumber = cNumber.replaceAll(" ", "");
                                                    Log.d("number", cNumber);
                                                }
                                            } else {
                                                cNumber = contactNumbers;
                                                cNumber = cNumber.replaceAll(" ", "");
                                                Log.d("without + number", cNumber);
                                            }
//                                            break;
                                    }
                                }
                            }
                            break;
                        } while (dataCursor.moveToNext()); // Now move to next


                        if (!cNumber.equals("")) {
                            contact = new Contact();
                            contact.setContctId(String.valueOf(contctId));
                            contact.setContactNumber(cNumber);
                            contact.setContactName(displayName);
                            contact.setContactImg(imnage);
                            contact.save();

                            contactList.add(new Contact_Model(displayName, cNumber, imnage));// Finally add
                        } else {
                            Log.d("Contact : ", "Don't add empty contact");
                        }
                    }

                } while (contactsCursor.moveToNext());
            }      

I also tried LIMIT 50 in cursor. but when i use LIMIT no contacts available

Cursor contactsCursor = getContentResolver().query(uri,
                    null, null, null, "LIMIT 10, " + count);

count += 10;
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Unnati Patadia
  • 662
  • 3
  • 19
  • 39
  • fetch all the contacts at first in your list, then iterate list with 50 items per each one by one , thats your logic how you do that – Quick learner Sep 26 '18 at 05:25
  • also fetch all the contacts then sort it with alphabetic order and then start iterating 50 contacts each one by one – Quick learner Sep 26 '18 at 05:26
  • When i remove for loop it gives me all contacts in alphabetic order @Quicklearner – Unnati Patadia Sep 26 '18 at 05:30
  • this is an inefficient way of getting data about all contacts on the device, you need to query Data table directly, see https://stackoverflow.com/a/50468411/819355 for example code – marmor Sep 27 '18 at 06:48

1 Answers1

1

Way 1

If you want pagination in query then use offset

final Cursor c = getContentResolver.query(uri,null,null,null," LIMIT 50 OFFSET 2");

Hold value of last offset and send ++offset the next time.

Way 2

You can get all contacts at once, and then set in list by using pagination. @Related question.

Suggestion

I faced this problem also, when contacts are too much eg. 500-1000, then it takes some seconds to fetch.

I solved it by fetching contact list before that activity appear. I fetched contacts and hold them in Application class, and cleared after use (to remove memory leak).

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • I think 500-1000 contacts is NOTHING. I am working on something and about 40% of my users have more than 5000 contacts. Some have 22K+ contacts. Crazy! – Aman Gautam Sep 10 '20 at 14:48