我正在开发一个包括处理联系人的应用程序。我尝试了不同的方法,但没有一个工作..我在一个标签的活动片段中获取联系人详细信息。它正确提取所有其他详细信息,除了联系人的电子邮件地址。我找出了原因,因为ContactsContract.CommonDataKinds.Phone._ID
获取了不同于ContactsContract.CommonDataKinds.Email.CONTACT_ID
的ID。现在我应该编码以获取电话中的所有联系人以及name
,number
,{ {1}},email
。请帮忙!这是我的代码:
photo_uri
答案 0 :(得分:0)
获取电子邮件地址电子邮件,
String email = managedCursor .getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
试试这个。
答案 1 :(得分:0)
您可以使用此方法获取该联系人的电子邮件ID。在您的情况下,您可以直接传递联系人ID。
public ArrayList<String> getEmail() {
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
答案 2 :(得分:0)
您的代码存在一些混淆,您无法查询Phone.CONTENT_URI
并希望获得电子邮件地址,因为电子邮件存储在Email.CONTENT_URI
中。
天真的方式是在Email.CONTENT_URI
上添加另一个查询,但这不是必需的,您最好的选择是查询包含电子邮件和电话的整个Data.CONTENT_URI
表格(以及很多其他类型的信息),并只要求mimetypes用于电子邮件和电话。
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Email.ADDRESS, Phone.NUMBER };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, Data.MIMETYPE + " IN ('" + Email.CONTENT_ITEM_TYPE + "' , '" + Phone.CONTENT_ITEM_TYPE +"')", null, null);
while ((cur != null) && cur.moveToNext()) {
long contactId = cur.getLong(0);
String name = cur.getString(1);
String type = cur.getString(2);
String email = null;
String phone = null;
if (type == Email.CONTENT_ITEM_TYPE) {
email = cur.getString(3);
} else {
phone = cur.getString(4);
}
// do whatever you want with above variables.
}
注意:对于为该联系人存储的每个电话/电子邮件,您将获得相同的contactId
和name
,因此您应该使用HashMap
来map contactId到详细信息列表,以便存储所有这些数据。
您首先应该先阅读有关ContactsContract API以及Contacts和Data表的文档,因为这些对于了解如何查询联系人信息至关重要。
答案 3 :(得分:0)
最后我解决了。我已将contact_id
从ContactsContract.CommonDataKinds.Phone._ID
更改为ContactsContract.CommonDataKinds.Phone.CONTACT_ID
...我的更新代码为:
public class FragmentContacts extends Fragment{
ListView contacts;
ArrayList<String> namelist=new ArrayList<String>();
ArrayList<String> number=new ArrayList<String>();
ArrayList<String> contact_id=new ArrayList<String>();
ArrayList<String> contact_image_uri=new ArrayList<String>();
ArrayList<String> contact_email=new ArrayList<String>();
ImageButton addContact;
String email;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
contacts=(ListView)rootView.findViewById(R.id.contact_list);
addContact=(ImageButton)rootView.findViewById(R.id.add_contact);
namelist.clear();
getContacts();
addContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNewContact();
}
});
return rootView;
}
public void getContacts() {
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor managedCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
while (managedCursor.moveToNext()) {
String id = managedCursor.getString(0);
String name = managedCursor.getString(1);
String phoneNumber=managedCursor.getString(2);
String image_uri=managedCursor.getString(3);
email=getEmail(id);
contact_id.add(id);
namelist.add(name);
number.add(phoneNumber);
contact_image_uri.add(image_uri);
contact_email.add(email);
}
managedCursor.close();
ContactAdapter a = new ContactAdapter(getActivity(), contact_id, namelist, number,contact_image_uri,contact_email);
contacts.setAdapter(a);
}
public void addNewContact()
{
Intent in= new Intent(getContext(),AddContact.class);
startActivity(in);
}
private String getEmail(String contactId) {
String mailE=null;
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = ?",
new String[]{contactId}, null);
while (cursor.moveToNext())
{
mailE=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
}
cursor.close();
return mailE;
}
}