简体   繁体   中英

event_start always returning contact number in android

Why ContactsContract.CommonDataKinds.Event.START_DATE always returning contact number. The number is available in Google Contacts.

Here what I am doing...

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));

For setting in layout.

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

I can't see any problem anywhere. API level 22 maybe. Android version : 5.1.

Your query is running on the Phone.CONTENT_URI table, which can only return basic Contacts table columns + Phone specific columns, it can't for example return Event.START_DATE data.

The Phone.Number constant and the Event.START_DATE constant both map to the underling field ContactsContract.DataColumns.DATA1 (see Phone columns here ) - that is why you're getting the phone number in your code.

To get information from multiple contact data tables you need to query on Data.CONTENT_URI table instead, which will return data from all data types and then use the mimetype value to figure out which type of row you are currently on.

So if you need only Phone and Event data types, here's the code:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM