gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:45:54 26 4
gpt4 key购买 nike

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

        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;
}

最佳答案

使用此代码查找所有与您的联系人链接的电子邮件只需传递 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;
}

关于android - 如何在联系人的详细信息中获取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com