阅读联系人电话和电子邮件

时间:2016-09-01 12:43:36

标签: android listview android-contacts phone-number

我使用下面的代码阅读有电话号码和电子邮件的联系人!它会成功读取所有联系人,但如果您联系中没有电子邮件,则会显示上一封联系人的电子邮件。

如下图所示!有任何想法吗?? 提前谢谢

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

1 个答案:

答案 0 :(得分:0)

这与变量的范围有关。您在String email之外声明了while(cursor.moveToNext())。当您的联系人没有任何电子邮件时,while (emailCursor.moveToNext())将不会更新String email的值,这意味着当您执行contact.setContactEmail(email);时,它仍然包含上一次联系的值。< / p>