我正在尝试从 ContactsContract 获取电子邮件地址,但我只得到一个空字符串!姓名和号码工作正常,但不是电子邮件地址!
我很困惑,几天来一直在尝试解决这个问题,但我不知道出了什么问题,如果我错过了代码中的某些内容或者我不知道如何解决这个问题。
应用程序的这一部分具有来自 ContactsContract 的所有名称的 ListView。在 ListView 中选择姓名时,代码应从 ContactsContract 中获取姓名、号码和电子邮件地址。
我将不胜感激能够继续工作的一些帮助!也许有更好的方法来解决这个问题,那么请告诉我如何改变!谢谢!
由于 toast 消息没有出现在 while 循环中,while(cursorEmail.moveToFirst()){....
我猜电子邮件光标有问题!?好像是空的!?
public class Activity_3 extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
listView = (ListView) findViewById(R.id.contactList);
String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID };
Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
// From column
String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
// To view
int[] toView = { R.id.contactItem };
startManagingCursor(cursor1);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
};
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
cursor.moveToPosition(position);
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String pos = Integer.toString(position);
String contactEmailAddress = "?";
//Email
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null);
while(emails.moveToNext()){
contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
}
emails.close();
Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();
}
});
}
}