【问题标题】:event_start always returning contact number in androidevent_start 总是在android中返回联系人号码
【发布时间】:2021-11-12 22:17:52
【问题描述】:

为什么ContactsContract.CommonDataKinds.Event.START_DATE 总是返回联系电话。该号码可在 Google 通讯录中找到。

这是我在做什么......

Cursor c;
        if (contactID=="") {
            c = activity.getContentResolver().query(Phone.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
        }else{
            StringBuffer whereClause = new StringBuffer();
            whereClause.append(Phone.CONTACT_ID);
            whereClause.append("=");
            whereClause.append(contactID);
            c = activity.getContentResolver().query(Phone.CONTENT_URI, null, whereClause.toString(), null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
        }
birthday = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));

用于布局中的设置。

birthday = fetchSpecificContact.get(Constants.BIRTHDAY);
etxtBirthday.setText(birthday);

我在任何地方都看不到任何问题。 API 级别 22 可能。安卓版本:5.1。

【问题讨论】:

    标签: java android contacts android-contacts android-cursor


    【解决方案1】:

    您的查询在 Phone.CONTENT_URI 表上运行,它只能返回基本的联系人表列 + 电话特定列,例如不能返回 Event.START_DATE 数据。

    Phone.Number 常量和 Event.START_DATE 常量都映射到下属字段 ContactsContract.DataColumns.DATA1(请参阅 Phone columns here) - 这就是您在代码中获取电话号码的原因。

    要从多个联系人数据表中获取信息,您需要查询 Data.CONTENT_URI 表,这将返回来自所有数据类型的数据,然后使用 mimetype 值来确定哪种类型的行您目前正在使用。

    因此,如果您只需要电话和事件数据类型,代码如下:

    String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1}; // if you need more fields add them here
    
    // query only phones / events
    String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Event.CONTENT_ITEM_TYPE + "')";
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
    
    while (cur != null && cur.moveToNext()) {
        long id = cur.getLong(0);
        String name = cur.getString(1); // full name
        String mime = cur.getString(2); // type of data (phone / event)
        String data = cur.getString(3); // the actual info
    
        Log.d(TAG, "got " + id + "-" + name + "-" + mime + ": " + data);
    
        if (mime == Phone.CONTENT_ITEM_TYPE) {
            // do something with the phone
        } else {
            // do something with the event start_date
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      相关资源
      最近更新 更多