如何在联系人的详细信息中获取电子邮件地址

How to get email address in the details of a contact

我正在尝试开发一个应用程序,它将从 phone 中读取联系人并在列表视图中显示联系人姓名。单击列表视图的项目时,联系人的详细信息(例如姓名、电子邮件地址和 phone 号码)将显示在另一个 activity 中。我可以显示姓名和 phone 号码,但无法显示电子邮件地址。任何人都可以帮助进行必要的更改以读取电子邮件地址并将其显示在联系方式中吗?

        var cursor : Cursor? = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,null,null,null)
            startManagingCursor(cursor)
        var selected_columns = arrayOf(
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone._ID)
        var to =  intArrayOf(android.R.id.text1)
        simple = SimpleCursorAdapter(
                this,android.R.layout.simple_list_item_1,cursor,selected_columns,to,0)
        my_listview.adapter = simple

        my_listview.setOnItemClickListener { parent, view, position, id ->

                //Log.i("Details","Opened")
            val name =  cursor?.getString(cursor?.getColumnIndex(selected_columns[0]))
            val phone =  cursor?.getString(cursor?.getColumnIndex(selected_columns[1]))
            val intent = Intent(this, Contactdetails::class.java)
            intent.putExtra("personname",name)
            intent.putExtra("personnumber",phone)
            startActivity(intent)

        }


    #Edited and added the function to get emails
    fun getemails(context:Context):ArrayList<String>{
        val emails = ArrayList<String>()
        var mEmail = ""
        val mEmailCursor: Cursor? = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            null, ContactsContract.CommonDataKinds.Email.CONTACT_ID,null, null);
        if (mEmailCursor!!.getCount() > 0) {
            while (mEmailCursor.moveToNext()) {
                mEmail = mEmailCursor.getString(mEmailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).trim();
                if (mEmail != null) {
                    emails.add(mEmail);
                }
            }
        }
        mEmailCursor.close();
        return emails;
    }

使用此代码查找您的联系人的所有电子邮件 link 只需传递 contact_id

public ArrayList<String> getEmail(Context context, String contact_id) {
    ArrayList<String> emails = new ArrayList<>();

    String mEmail = "";
    Cursor mEmailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?", new String[]{contact_id}, null);
    if (mEmailCursor.getCount() > 0) {
        while (mEmailCursor.moveToNext()) {
            mEmail = mEmailCursor.getString(mEmailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).trim();
            if (mEmail != null) {
                emails.add(mEmail);
            }
        }
    }
    mEmailCursor.close();
    return emails;
}
    fun getemails(context:Context):ArrayList<String>{
    val emails = ArrayList<String>()
    var mEmail = ""
    val mEmailCursor: Cursor? = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        null, ContactsContract.CommonDataKinds.Email.CONTACT_ID,null, null);
    if (mEmailCursor!!.getCount() > 0) {
        while (mEmailCursor.moveToNext()) {
            mEmail = mEmailCursor.getString(mEmailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).trim();
            if (mEmail != null) {
                emails.add(mEmail);
            }
        }
    }
    mEmailCursor.close();
    return emails;
}