'How to fetch 50 by 50 contacts from contact list?

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;


Solution 1:[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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community