我想从电话簿中检索电子邮件,电话号码和联系人姓名,并在列表视图中显示。
名称和数字完全,但电子邮件无法检索。
以下是相关代码:
public void getAllContacts(ContentResolver cr) {
String email="";
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, Phone.DISPLAY_NAME + " ASC");
//email = new String[phones.getCount()];
while (phones.moveToNext())
{
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("name.................."+name+"........number......."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if(emailCur!=null)
{
emailCur.moveToFirst();
}
// emailCur.moveToFirst();
while (emailCur.moveToNext()) {
email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println(email);
}
email1.add(email);
emailCur.close();
}
phones.close();
}
我知道之前已经问过这个问题,但没有一个解决方案对我有用。所以有人能指出我在这里犯的错误吗?
答案 0 :(得分:3)
我遇到了同样的问题。我通过改变kgandroid的代码来解决它
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
到
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
(注意ContactsContract.CommonDataKinds.Phone.CONTACT_ID =“contact_id”,而ContactsContract.Contacts._ID =“_ id”)
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
public ArrayList<HashMap<String,String>> getAllContacts(ContentResolver cr) {
ArrayList<HashMap<String,String>> contactList = new ArrayList<HashMap<String, String>>();
Cursor phones = cr.query(Phone.CONTENT_URI, null,null,null, Phone.DISPLAY_NAME+ " ASC");
while (phones.moveToNext())
{
HashMap<String,String> row = new HashMap<String, String>();
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
String name=phones.getString(phones.getColumnIndex(Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(Phone.NUMBER));
System.out.println("name.................."+name+"........number......."+phoneNumber);
row.put("Name",name);
row.put("PhoneNo",phoneNumber);
row.put("Email",getEmail(cr,id));
contactList.add(row);
}
phones.close();
return contactList;
}
private static String getEmail(ContentResolver cr,String contactId) {
String emailStr = "";
final String[] projection = new String[]{Email.DATA, Email.TYPE};
final Cursor email = cr.query(Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?",
new String[]{contactId}, null);
if (email.moveToFirst()) {
final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);
while (!email.isAfterLast()) {
emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
email.moveToNext();
}
}
email.close();
return emailStr;
}
Note : add this <uses-permission android:name="android.permission.READ_CONTACTS"/> in AndroidManifest.xml
答案 2 :(得分:0)
试试这段代码:
public void getAllContacts(ContentResolver cr) {
String email="";
// Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, Phone.DISPLAY_NAME + " ASC");
Cursor phones = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, Phone.DISPLAY_NAME + " ASC");
//email = new String[phones.getCount()];
while (phones.moveToNext()){
String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("name.................."+name+"........number......."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null
, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"
, new String[]{id}, null);
if(emailCur!=null){
emailCur.moveToFirst();
}
// emailCur.moveToFirst();
while (emailCur.moveToNext()) {
email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println(email);
}
email1.add(email);
emailCur.close();
}
phones.close();
}