我使用以下代码从android检索联系人名字和姓氏列表但是我也想同时检索电子邮件:
Cursor1 = getContentResolver().query(
public List<String> getFullContactName()
{
List<String> name = new ArrayList<String>();
String[] projection = new String[] {Data.DATA2, Data.DATA3};
String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
Uri uri = Data.CONTENT_URI;
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Cursor cursor = contentResolver.query(uri,projection,where,null,null);
String firstName, lastName;
while (cursor.moveToNext())
{
firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);
Toast.makeText(getApplicationContext(), "First name"+firstName, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Second name"+lastName, Toast.LENGTH_LONG).show();
}
cursor.close();
cursor = null;
return name;
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
cursor.close();
我需要知道如何使用单个光标一起读取名字,姓氏电子邮件,电话号码,因为我需要在列表中获取详细信息,然后在列表视图中显示它们。我没有参考,我可以用一个光标获取细节。
答案 0 :(得分:0)
删除
cursor.close();
cursor = null;
并确保您传递给contactId
的{{1}}与您从Cursor emails
检索到的Cursor cursor
相同,而String[] projection
似乎没有在String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
中声明它1}}
对于第一次投影,您可以如下:
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String Name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
此投影将检索联系人ID和联系人姓名,然后获取联系人ID和姓名
String[] projection2= new String[]{
Email.ADDRESS,
Email.TYPE
然后第二个投影可能是某事:
Cursor cur2 = cr.query(emailURI,projection2,Email.CONTACT_ID + "=" +id,null,null);
然后定义第二个光标以在第一个光标内获取电子邮件,如
{{1}}
其中id是您从第一个游标获得的ID。
希望这个帮助
答案 1 :(得分:0)
使用以下代码获取联系方式。
public void getContacts(ContentResolver cr) {
ArrayList<HashMap<String, String>> mContacts = new ArrayList<HashMap<String, String>>();
HashMap<String, String> mTempObj = new HashMap<String, String>();
cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Email", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Number", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Name", cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
cur.moveToNext();
}
}
cur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null, null, null, null);
cur.moveToFirst();
if (cur.getCount() > 0) {
for (int i = 0; i < cur.getCount(); i++) {
mTempObj.add("Address", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
mTempObj.add("City", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
mTempObj.add("State", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
cur.moveToNext();
}
}
mContacts.add(mTempObj);
}
它将解决您的问题。