【问题标题】:Read Contacts phone and email阅读联系人电话和电子邮件
【发布时间】:2017-01-09 08:39:34
【问题描述】:

我正在使用下面的代码来读取带有电话号码和电子邮件的联系人!它成功读取所有联系人,但如果您的联系人没有电子邮件,它会显示前一个联系人的电子邮件。

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

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

【问题讨论】:

标签: android listview android-contacts phone-number


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 2023-04-04
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多