从联系人表中选择/加入表但没有输出 - Android

时间:2014-01-22 11:41:06

标签: android sqlite contactscontract

我正在尝试使用contactsraw_contactsdata表格覆盖所有联系人。

通过在线阅读,我认为Contacts._idRawContacts.contact_id相关联,Data.raw_contact_idRowContacts._id相关联。我是对的吗?

相信我建立了这个方法:

public void testingContactsDatabase(String contactId)
    {
        final String[] projection = new String[] {
                Contacts._ID,
                Contacts.DISPLAY_NAME,
                Data.RAW_CONTACT_ID,
                Data.MIMETYPE,
                StructuredName.DISPLAY_NAME, 
                StructuredName.FAMILY_NAME,
                StructuredName.GIVEN_NAME, 
                StructuredName.MIDDLE_NAME,
                StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
                StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
                };

        final String selection = Contacts._ID + " = " + contactId + " AND " 
                    + Contacts._ID + " = " + RawContacts.CONTACT_ID + " AND "
                    + Data.RAW_CONTACT_ID + " = " + RawContacts._ID;

        final Cursor curStructuredName = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI, 
                projection, 
                selection, null,
                //new String[] {contactId, ContactsContract.RawContacts.CONTACT_ID, RawContacts._ID},
                null
                );

        if(curStructuredName.moveToFirst())
        {
            Log.d("XYZ", "Found something");
        }
    }

但它永远找不到任何东西。我不确定我是否使用了正确的URI,我尝试过使用不同的URIs,并且获得了例外或相同的输出。

有人能指出我在哪里做错了吗?

由于

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。这是我现在的代码:

// Get the structured name fields
    public ArrayList<String> getStructuredName(String contactId)
    {
        ArrayList<String> structuredList = new ArrayList<String>();

        final String[] projection = new String[] {
                StructuredName.DISPLAY_NAME, 
                StructuredName.FAMILY_NAME,
                StructuredName.GIVEN_NAME, 
                StructuredName.MIDDLE_NAME,
                StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
                StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
        };

        final String filter = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?"; 
        final String parameters[] = {StructuredName.CONTENT_ITEM_TYPE, contactId};

        Cursor cursor =  context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
                projection, filter, parameters, null);

        if(cursor.moveToFirst())
        {
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                final String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
                final String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
                final String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
                final String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
                final String prefix = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
                final String suffix = cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX));

                structuredList.add(displayName);
                structuredList.add(familyName);
                structuredList.add(givenName);
                structuredList.add(middleName);
                structuredList.add(prefix);
                structuredList.add(suffix);

                Log.d(tag, "/////////////////////////////////////////////////////");
                Log.d(tag, "displayName: " + displayName );
                Log.d(tag, "familyName: " + familyName );
                Log.d(tag, "givenName: " + givenName );
                Log.d(tag, "middleName: " + middleName );
                Log.d(tag, "prefix: " + prefix );
                Log.d(tag, "suffix: " + suffix );
                Log.d(tag, "/////////////////////////////////////////////////////");

            }
        }

        cursor.close();
        return structuredList;

    }