0

I am working with android device contacts. if android device contacts more than five thousand,Fetch data from contacts take too much time with blank screen. I have used bellow code to fetch data

private void fetchContacts1() {
    String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, order);
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    }
 cursor.close();
}

I show all the contacts to recycle view.There are some devices which has more than ten thousand contacts. can you suggest me how to handle it.

Enamul Haque
  • 3,249
  • 1
  • 20
  • 32
  • I'd suggest you store the cursor as a field in your class, and return only perhaps 100 at a time. And then, you'd keep asking for more as long as you haven't hit the end. Along with a progress bar or cancel dialog, this gives the user some feedback and allows them to cancel. – user2740650 Jan 20 '20 at 02:28
  • @user2740650 can you provide example l – Enamul Haque Jan 20 '20 at 02:33

1 Answers1

2

Use the Async task to load your contacts in the background. after loading for first-time store your list of contacts to your local database

below following links may help you

https://developer.android.com/guide/components/loaders

https://stackoverflow.com/a/40017905/10239870

  class ContactLoader extends AsyncTask<Void, Void, List<Contact>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //visible your progress bar here
    }

    @Override
    protected List<Contact> doInBackground(Void... voids) {
        List<Contact> contacts = new ArrayList<>();
        String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, order);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts.add(new Contact(name, phonenumber));
        }
        cursor.close();
        return contacts;
    }

    @Override
    protected void onPostExecute(List<Contact> contactList) {
        super.onPostExecute(contactList);
        //set list to your adapter
    }
}


  class Contact {
    String name;
    String number;

    public Contact(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
Arunachalam k
  • 582
  • 1
  • 6
  • 16