ContactsContract.CommonDataKinds.Event.START_DATE 包括联系人 phone 号码
ContactsContract.CommonDataKinds.Event.START_DATE includes contact phone number
我使用以下代码检索联系人的所有事件:
Uri uri = ContactsContract.Data.CONTENT_URI;
ContentResolver cr = context.getContentResolver();
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.DATA3};
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_OTHER + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_CUSTOM;
String[] selectionArgs = new String[]
{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
问题是联系人的 phone 号码被列为 Event.START_DATE 并且带有 TYPE_OTHER。
有什么方法可以排除它们,只获取真实事件吗?
我认为您应该在 where
子句中添加澄清括号,因为它现在可以包含具有非事件 mime 类型(例如 CommonDataKinds.Phone
)的行,只要它们具有 TYPE = CUSTOM。
试试这个:
String where = Data.MIMETYPE + "= ? AND "
+ "("
+ Event.TYPE + "="
+ Event.TYPE_ANNIVERSARY + " OR "
+ Event.TYPE + "="
+ Event.TYPE_OTHER + " OR "
+ Event.TYPE + "="
+ Event.TYPE_CUSTOM
+ ")";
String[] selectionArgs = new String[]
{Event.CONTENT_ITEM_TYPE};
private ArrayList<String> getEventday(String contactId) {
String eventday = null;
ArrayList<String> eventlist = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.DATA3};
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? AND"
+ "("
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_OTHER
+ ")";
String[] selectionArgs = new String[]
{contactId,ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
Cursor pCur = cr.query(uri, projection, orgWhere, selectionArgs, sortOrder);
while (pCur.moveToNext()) {
String eventytype = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
eventday = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
ContactAddPhoneBook contactAddPhoneBook=new ContactAddPhoneBook();
String typeEvent=contactAddPhoneBook.getContentTypeCustomerlistEventtype(Integer.parseInt(eventytype));
eventlist.add("(" + typeEvent + ")" + ":" +eventday);
}
pCur.close();
return eventlist;
}
我使用以下代码检索联系人的所有事件:
Uri uri = ContactsContract.Data.CONTENT_URI;
ContentResolver cr = context.getContentResolver();
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.DATA3};
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_OTHER + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_CUSTOM;
String[] selectionArgs = new String[]
{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
问题是联系人的 phone 号码被列为 Event.START_DATE 并且带有 TYPE_OTHER。
有什么方法可以排除它们,只获取真实事件吗?
我认为您应该在 where
子句中添加澄清括号,因为它现在可以包含具有非事件 mime 类型(例如 CommonDataKinds.Phone
)的行,只要它们具有 TYPE = CUSTOM。
试试这个:
String where = Data.MIMETYPE + "= ? AND "
+ "("
+ Event.TYPE + "="
+ Event.TYPE_ANNIVERSARY + " OR "
+ Event.TYPE + "="
+ Event.TYPE_OTHER + " OR "
+ Event.TYPE + "="
+ Event.TYPE_CUSTOM
+ ")";
String[] selectionArgs = new String[]
{Event.CONTENT_ITEM_TYPE};
private ArrayList<String> getEventday(String contactId) {
String eventday = null;
ArrayList<String> eventlist = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.DATA3};
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ? AND"
+ "("
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " OR "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_OTHER
+ ")";
String[] selectionArgs = new String[]
{contactId,ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
String sortOrder = null;
Cursor pCur = cr.query(uri, projection, orgWhere, selectionArgs, sortOrder);
while (pCur.moveToNext()) {
String eventytype = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
eventday = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
ContactAddPhoneBook contactAddPhoneBook=new ContactAddPhoneBook();
String typeEvent=contactAddPhoneBook.getContentTypeCustomerlistEventtype(Integer.parseInt(eventytype));
eventlist.add("(" + typeEvent + ")" + ":" +eventday);
}
pCur.close();
return eventlist;
}