我需要通过电子邮件获取联系人信息(光标)。它们必须是截然不同的如果他收到电子邮件,每个联系人必须有一个条目。怎么做?我的目标是新的联系人API附带2.0。
1)我尝试使用CursorJoiner来做,但是发生了一件奇怪的事情。这是我的代码:
MatrixCursor matCur = new MatrixCursor(
new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
"photo_id",
"starred"
}
);
Cursor newContactCursor = managedQuery(
ContactsContract.Contacts.CONTENT_URI,
new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
"photo_id",
"starred"
},
null,
null,
null//Contacts._ID
);
newContactCursor.moveToFirst();
Cursor emailCur = managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {
Email.CONTACT_ID,
Email.DATA1
},
null,
null,
Email.CONTACT_ID
);
emailCur.moveToFirst();
CursorJoiner joiner = new CursorJoiner(
newContactCursor,
new String[]{Contacts._ID},
emailCur,
new String[] {Email.CONTACT_ID}
);
for (CursorJoiner.Result joinerResult : joiner) {
switch (joinerResult) {
case LEFT:
// handle case where a row in cursorA is unique
//Log.i(TAG,"L|"+
//newContactCursor.getString(newContactCursor.getColumnIndex("_id")) );
break;
case RIGHT:
// handle case where a row in cursorB is unique
//Log.i(TAG,
//"R|"+
//emailCur.getString(emailCur.getColumnIndex("contact_id")) );
break;
case BOTH:
//Log.i(TAG,
//"L|"+
//newContactCursor.getString(newContactCursor.getColumnIndex("_id"))+
//"|R|"+
//emailCur.getString(emailCur.getColumnIndex("contact_id")) );
Log.i(TAG, newContactCursor.getString(newContactCursor.getColumnIndex("_id"))+"|"+
newContactCursor.getString(newContactCursor.getColumnIndex("display_name"))+"|"+
emailCur.getString(emailCur.getColumnIndex(Email.DATA1)));
String[] columnValues =
{newContactCursor.getString(newContactCursor.getColumnIndex("_id")),
newContactCursor.getString(newContactCursor.getColumnIndex("display_name")),
newContactCursor.getString(newContactCursor.getColumnIndex("photo_id")),
newContactCursor.getString(newContactCursor.getColumnIndex("starred"))
};
matCur.addRow(columnValues);
break;
}
}
现在我的问题是我输出如下: 在这个日志中它的_id | display_name |电子邮件ID 由于隐私问题,我已经更换了它们
1|[contact name]|[email id]
4|[contact name]|[email id]
5|[contact name]|[email id]
6|[contact name]|[email id]
7|
8|
9|
90|
91|
92|
93|
94|
95|
96|
97|
98|
99|
但你可以看到它直接从9跳到90然后全部9 9 9,这是什么?
2)我们可以使用distinct关键字吗?是否可以使用ContactsContract等联系提供商?
答案 0 :(得分:6)
尝试使用此代码段: 在列表视图中的同一行显示联系人姓名和电子邮件。
/**
* Populate the contact list based on account.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursorEmail = getContactsEmail();//get all emails
String[] fields = new String[] //fields of data to take
{ ContactsContract.Contacts._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.contact_entry, cursorEmail ,
fields, new int[]
{R.id.UID,R.id.contactEntryText,R.id.contactEmail});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContactsEmail()
{
// Run query
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1'";
//showing only visible contacts
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
答案 1 :(得分:2)
我遇到了同样的问题。我知道这个帖子很老了,但是这个答案可能会在将来帮助其他人。
您必须按MIME类型过滤才能删除重复项。我就这样做了:
Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection =
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ? AND " +
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?" + " AND " +
ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
mContactCursor = managedQuery(
contacts,
projection,
selection,
new String[] {"1", constraint.toString() + '%'},
sortOrder);
答案 2 :(得分:1)
以简单的方式做到这一点 首先找到contact_id,在此基础上我们将搜索与该联系人相关的所有email_id。 在任何按钮单击事件上写下此代码
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 2);
现在关于活动结果,
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
System.out.println("Request Code--"+requestCode);
super.onActivityResult(requestCode, resultCode, data);
if (data != null && requestCode == 2)
{
fromCurrent = true;
Uri uri = data.getData();
//fromCurrent=true;
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA ,
ContactsContract.CommonDataKinds.Email.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String id = c.getString(0);
String name = c.getString(1);
System.out.println("id "+id+" name:"+name);
ContactID = id;
retriveEmail();
if(arrEmail.size() == 0)
{
showToast("No Email Address found for the selected contact!");
}
else
{
ListFile = arrEmail.toArray(new String[arrEmail.size()]);
builder1 = new AlertDialog.Builder(ShareTheHeart_Activity.this);
builder1.setTitle("Select an email address :");
builder1.setSingleChoiceItems(ListFile,-1,new DialogInterface.OnClickListener() { //@Override
public void onClick(DialogInterface dialog, int which)
{
txtEmail.setText(ListFile[which]);
alert.cancel();
}
});
alert = builder1.create();
alert.show();
}
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
创建retriveEmail的方法,以这种方式在代码中写入,
private void retriveEmail()
{
try {
arrEmail = new ArrayList<String>();
String id = ContactID;
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI,
null, Email.CONTACT_ID + "=?", new String[] { id },
null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst())
{
do{
email = cursor.getString(emailIdx);
arrEmail.add(email);
Log.v("ABC", "Got email: " + email);
} while(cursor.moveToNext());
} else {
Log.w("ABC", "No results");
}
} catch (Exception e) {
Log.e("ABC", "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
就是这样。
请理解代码,不要只是复制和粘贴!