对多个数据源使用游标加载器

我有使用联系人姓名作为选择从 Android 获取联系人信息的代码。但是,数据源(我猜是 ContactsContract.Contacts)并没有我需要的有关联系人的所有信息。我看到联系信息分为几个表(ContactsContract.CommonDataKinds.StructuredName、ContactsContract.CommonDataKinds.Phone 等)。我还注意到所有这些来源都可以访问 LOOKUP_KEY 字段。它唯一标识一个联系人。我想知道是否有办法使用 LOOKUP_KEY 对这些数据源执行连接。


下面,我目前为单个数据源设置投影和选择,并使用游标加载器异步查询它。有没有办法使用 LOOKUP_KEY 一次搜索多个数据源?


private static final String[] CONTACT_PROJECTION = {

        ContactsContract.Contacts._ID,

        ContactsContract.Contacts.LOOKUP_KEY,

        ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,

        ContactsContract.Contacts.HAS_PHONE_NUMBER

};


private static final String CONTACT_SELECTION = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY

        + " LIKE ?";


public static Loader<Cursor> getContactLoader(Context applicationContext, String searchString) {

    String[] selectionArgs = new String[1];

    selectionArgs[0] = "%" + searchString + "%";

    return new CursorLoader(

            applicationContext,

            ContactsContract.Contacts.CONTENT_URI,

            CONTACT_PROJECTION,

            CONTACT_SELECTION,

            selectionArgs,

            null

    );

}


DIEA
浏览 76回答 1
1回答

PIPIONE

所有联系人的所有信息都分组在别名表中,这些表实际上存储在一个名为 的大表中,因此您可以简单地查询该表以获取与Data特定联系人相关的所有信息(您可以使用), 就像这样:CONTACT_IDLOOKUP_URIpublic void getContactInfo(long contactId) {&nbsp; &nbsp; Uri uri = Data.CONTENT_URI;&nbsp; &nbsp; String[] projection = new String[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data.CONTACT_ID,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data.DISPLAY_NAME,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data.MIMETYPE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Data.DATA1}; // you can get additional info if needed in Data.DATA2, DATA3, etc.&nbsp; &nbsp; // get all relevant info about the contact&nbsp; &nbsp; String selection = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + " IN (?,?,?,?)"; // you can add more question marks if you need more MIMETYPES&nbsp; &nbsp; String[] selectionArgs = new String[]{String.valueOf(contactId), Phone.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE, Event.CONTENT_ITEM_TYPE, StructuredPostal.CONTENT_ITEM_TYPE};&nbsp; &nbsp; Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);&nbsp; &nbsp; while (cursor.moveToNext()) {&nbsp; &nbsp; &nbsp; &nbsp; String name = cursor.getString(1);&nbsp; &nbsp; &nbsp; &nbsp; String mimetype = cursor.getString(2);&nbsp; &nbsp; &nbsp; &nbsp; String data = cursor.getString(3);&nbsp; &nbsp; &nbsp; &nbsp; // use the mimetype to figure out what kind of info is in this row&nbsp; &nbsp; &nbsp; &nbsp; Log.i("Contact Info", contactId + "=> " + name + " - " + mimetype + ": " + data);&nbsp; &nbsp; }&nbsp; &nbsp; cursor.close();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java