-1

I try to make Contact App and now I try to read the Contacts I have 450 contacts and its take something like 60 sec to read them all.

I do it with Contact class:

public class Contact {
String name;
public ArrayList<String> PhoneNumber =  new ArrayList<>();
public ArrayList<String> Email = new ArrayList<>();
int NumberOfPhones = 0;
int NumberOfMails = 0 ;
}

and I read like this :

tatic final int CODE_FOR_PERMISSION = 123;
List<Contact> ListContact = new ArrayList<Contact>();
Contact TempContact;
String TempName ="";
ArrayList<String> TempPhoneNumber = new ArrayList<>();
public ArrayList<String> TempEmail = new ArrayList<>();
int TempCounter = 0;
private ProgressDialog pDialog;
private Handler updateBarHandler;
ArrayList<String> contactList;
Cursor cursor;
int counter;
TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_activity);
    AskForPermission();

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Reading contacts...");
    pDialog.setCancelable(false);
    pDialog.show();

    updateBarHandler = new Handler();

    // Since reading contacts takes more time, let's run it on a separate thread.
    new Thread(new Runnable() {

        @Override
        public void run() {
            getContacts();
        }
    }).start();
    //init();
    //OnAction();




void init(){
    textView = (TextView) findViewById(R.id.chekk);


}

void OnAction(){

  TempContact.PrintToTextView(textView,ListContact);




}




public void getContacts() {

    contactList = new ArrayList<String>();

    String phoneNumber = null;
    String email = null;

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    String _ID = ContactsContract.Contacts._ID;
    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

    Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
    String DATA = ContactsContract.CommonDataKinds.Email.DATA;

    StringBuffer output;

    ContentResolver contentResolver = getContentResolver();

    cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

    // Iterate every contact in the phone
    if (cursor.getCount() > 0) {

        counter = 0;
        while (cursor.moveToNext()) {

            output = new StringBuffer();

            // Update the progress message
            updateBarHandler.post(new Runnable() {
                public void run() {
                    pDialog.setMessage("Reading contacts : " + counter++ + "/" + cursor.getCount());
                }
            });

            String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
            String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));

            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

            if (hasPhoneNumber > 0) {

                output.append("\n First Name:" + name);
                TempName = name;

                //This is to read multiple phone numbers associated with the same contact
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);

                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Phone number:" + phoneNumber);
                    TempPhoneNumber.add(phoneNumber);
                }

                phoneCursor.close();

                // Read every email id associated with the contact
                Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID + " = ?", new String[]{contact_id}, null);

                while (emailCursor.moveToNext()) {

                    email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
                    TempEmail.add(email);
                    output.append("\n Email:" + email);

                }

                emailCursor.close();

                String columns[] = {
                        ContactsContract.CommonDataKinds.Event.START_DATE,
                        ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.MIMETYPE,
                };

                String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
                        " and " + ContactsContract.CommonDataKinds.Event.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' and " + ContactsContract.Data.CONTACT_ID + " = " + contact_id;

                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;

                Cursor birthdayCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder);
                Log.d("BDAY", birthdayCur.getCount()+"");
                if (birthdayCur.getCount() > 0) {
                    while (birthdayCur.moveToNext()) {
                        String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                        output.append("Birthday :" + birthday);
                        Log.d("BDAY", birthday);
                    }
                }
                birthdayCur.close();
            }

            // Add the contact to the ArrayList
            contactList.add(output.toString());
            TempContact = new Contact(TempName,TempPhoneNumber,TempEmail);
            ListContact.add(TempContact);
            TempName = "";
            TempPhoneNumber.clear();
            TempEmail.clear();
        }



        // Dismiss the progressbar after 500 millisecondds
        updateBarHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                pDialog.cancel();
            }
        }, 5);
    }

}

how I can make the reading from contacts faster? I have searched in other sutes but I don't know how to do it faster. it can be done because the default contact app in the phone makes it very fast.

Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
  • 1
    Hi, welcome to SO. Please read **[how to create a minimal, verifiable example](https://stackoverflow.com/help/mcve)** for better results using this site, and to avoid down-votes for pasting a wall of code. Good luck! – kenny_k Jan 25 '18 at 12:56

1 Answers1

0

You don't need to query for all contacts + all emails + all phones to display the main contacts list similar to the default contacts app. You'd probably notice that the main screen of all contacts apps display only names + picture, and not emails or phones. Just perform the first query in your code to display the list of contacts, and only when the user clicks on one of those contacts, you'll open a new activity with the details of that specific contact only (which is a short query to make)

marmor
  • 27,641
  • 11
  • 107
  • 150