从ContactProvider读取生日,无效列

时间:2016-07-13 13:30:12

标签: android android-contentprovider

我正在做一些测试,以弄清楚提供者如何处理事件等。

我正在尝试获取特定帐户的所有联系人,然后阅读每个联系人的详细信息以获取生日(如果有)。

我首先尝试在一开始就做到这一点,但我对提供者的知识不够,所以我试图分两步来知道我是否能够实现它。

我在此行中收到无效列: 光标游标= cr.query(uri,projection,[...] contact_id是这里的问题。如何改进第二个查询以获取我想要的数据?我尝试了无处不在的代码上的各种mod。

感谢。

我使用的代码:

private void readContacts(){
        Cursor contByAccCursor = getContactsByAccounts();       
        getContactsBirthdays(contByAccCursor);
    }

    private Cursor getContactsByAccounts() {
        Uri uri = ContactsContract.RawContacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.RawContacts.CONTACT_ID,
                ContactsContract.RawContacts.ACCOUNT_TYPE
        };
        String where = ContactsContract.RawContacts.ACCOUNT_TYPE + " IN ('com.google', 'vnd.sec.contact.phone', 'com.skype.contacts.sync')";
        return activity.getContentResolver().query(uri, 
                                                    projection, 
                                                    where, 
                                                    null, 
                                                    null);
    }

    private void getContactsBirthdays(Cursor filteredContacts) {
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = new String[] {
                //ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE
        };
        String where =
                ContactsContract.Data.MIMETYPE + "= ? AND " +
                ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
                ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " AND " +
                ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= ?";

        String[] selectionArgs;
        String sortOrder = null;

        StringBuffer sb = new StringBuffer();

        ContentResolver cr = activity.getContentResolver();

        int accTypeCol = filteredContacts.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);

        while(filteredContacts.moveToNext()){

            selectionArgs = new String[] { 
                    ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
                    filteredContacts.getString(filteredContacts.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID))
            };

            Cursor cursor = cr.query(uri, 
                        projection, 
                        where, 
                        selectionArgs, 
                        sortOrder);

            if(cursor.moveToNext()){                
                String dName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                String bDay = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                String accType = filteredContacts.getString(accTypeCol);

                sb.append(dName + "("+accType+")" + " - " + bDay);
                sb.append("\n");
            }
        }       
        Log.d(TAG, sb.toString());
    }

1 个答案:

答案 0 :(得分:0)

好的,我现在感觉很蠢。错误是第二个content_uri,我得错了,一定累了......

这是获取正确数据的正确uri:

private void getContactsBirthdays(Cursor filteredContacts) {
        Uri uri = ContactsContract.Data.CONTENT_URI;