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

Posted

技术标签:

【中文标题】从联系人表中选择/加入表但没有输出 - Android【英文标题】:Selecting/Joining tables from contacts table but get no ouput - Android 【发布时间】:2014-02-12 10:50:10 【问题描述】:

我正在尝试使用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】:

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

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

    

【讨论】:

以上是关于从联系人表中选择/加入表但没有输出 - Android的主要内容,如果未能解决你的问题,请参考以下文章

SQL查询存在A表但不存在B表的数据

SQL查询存在A表但不存在B表的数据

sql从多个表中获取值

MySQL加入分组没有完美加入

从相关表中获取每组的最大行数

将数据框加载到数据库表但保留现有的表结构