0

I'm using the code below to read contacts with phone number and email! It reads all contacts successfully but if you contact has no email, it displays the previous contact's email.

just as the picture below! Any ideas?? Thank you in advance

public void getContacts() {

    contactList = new ArrayList<Contacts>();
    String phoneNumber = null;
    String email = null;
    ContentResolver contentResolver = getContentResolver();


    String name = null;

    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    // Iterate every contact in the phone
    if (cursor.getCount() > 0) {
        counter = 0;

        while (cursor.moveToNext()) {

            // 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(ContactsContract.Contacts._ID));
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            int hasPhoneNumber = Integer.parseInt(cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
            if (hasPhoneNumber > 0) {

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

                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phoneCursor.close();
            }

            Cursor emailCursor = contentResolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                    + " = ?", new String[]{contact_id}, null);

            while (emailCursor.moveToNext()) {

                email = emailCursor.getString(emailCursor.getColumnIndex(
                        ContactsContract.CommonDataKinds.Email.DATA));
            }

            emailCursor.close();
            contact = new Contacts();

            contact.setContactFName(name);
            contact.setContactPhone(phoneNumber);
            contact.setContactEmail(email);

            contactList.add(contact);
        }

        // ListView has to be updated using a ui thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter = new ImportContactAdapter
                        (ImportContactActivity.this, R.layout.custom_import_contact, contactList);
                mListView.setAdapter(adapter);
            }
        });
        // Dismiss the progressbar after 500 millisecondds
        updateBarHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pDialog.cancel();
            }
        }, 500);
    }
}

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Possible duplicate of [Accessing the Contact name, Number and Email ID?](http://stackoverflow.com/questions/10993283/accessing-the-contact-name-number-and-email-id) – Vishal Patoliya ツ Sep 01 '16 at 12:53

1 Answers1

0

This has to do with the scope of your variables. You declare your String email outside of your while(cursor.moveToNext()). When your contact does not have any e-mail the while (emailCursor.moveToNext()) will not update the value of your String email, meaning that when you do contact.setContactEmail(email); it still contains the value of the previous contact.

AsheraH
  • 474
  • 10
  • 15