如何在单个请求中检索电话号码/带照片的身份证/名字/姓氏? [英] How to retrieve phone numbers/photo id/firstname/lastname within a single request?

查看:65
本文介绍了如何在单个请求中检索电话号码/带照片的身份证/名字/姓氏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此问题的答案:

I'm currently using the answer to this question: How to get all contacts first name, last name, email, phone number, etc without duplicates to retrieve the user contact list with, for each contact, all phone numbers, the firstname, lastname and the photo id.

该答案的问题是,每个所需数据创建一个请求.它在我的应用程序中造成了性能问题.我想从一个请求中检索所有这些信息,但是要理解Android的查询是如何管理的以及我要查找的存储数据的位置,我有很多困难.

The issue I've with that answer is it's creating one request per data wanted. It's creating performances issues on my app. I would like to retrieve all these information from a single request, but I've got a lot of difficulty to understand how Android's query are managed and where are stored data I'm looking for.

我尝试过的示例(我做了很多测试都没有成功,下面是我尝试过的最后一个测试):

Example of what I've tried (I've done a lot of test without success, following one is the last one I've tried):

    new CursorLoader(
        this,
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.Data.HAS_PHONE_NUMBER + "!=0 AND (" + ContactsContract.Data.MIMETYPE + "=?)",
        new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE},
        ContactsContract.Data.CONTACT_ID)
    ; 

我想检索的信息:

        int firstNameCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME);
        int lastNameCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME);
        int photoIdCol = cursor.getColumnIndex(CommonDataKinds.StructuredName.PHOTO_ID);
        int phoneCol = cursor.getColumnIndex(Phone.NUMBER);

该请求有两个问题:

  • 每个联系人只有一个电话号码,但是我的一些联系人有多个电话号码,我想找回它们

  • I only get one phone number per contact, however some of my contacts have several numbers and I would like to retrieve them

我没有检索名字和姓氏(我得到了一个奇怪的数字)

I'm not retrieving the firstname and the lastname (I get a weird number instead)

请注意,我不想检索DISPLAY_NAME,我既要给定名称又要取全名.另请注意,我希望每个联系人获得一个结果,而不是每个电话号码获得一个结果(即,一个结果包含一个或多个电话号码)

Please note that I don't want to retrieve the DISPLAY_NAME, I want both given_name and family_name. Please also note I would like to have one result per contact and not one result per phone number (i.e one result contain one to many phone numbers)

有一个想法可以在单个请求中完成吗?

Any idea to do that in a single request?

尝试使用@pskink的另一段代码,但出现不存在'_id'列"错误

trying with another piece of code from @pskink but I get a "column '_id' does not exists" error

    String[] projection = {
        ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
        ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
        ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.StructuredName.PHOTO_ID
    };
    String selection = ContactsContract.Data.MIMETYPE + " in (?, ?, ?, ?)";
    String[] selectionArgs = {
        ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
        ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
        ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.StructuredName.PHOTO_ID
    };
    //String sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE;

    //Uri uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI;
    Uri uri = ContactsContract.Data.CONTENT_URI;
    // ok, let's work...
    return new CursorLoader(
        this, uri, projection, selection, selectionArgs, null
    );

推荐答案

感谢@pskink,我现在有一个快速的单个请求来检索我想要的内容.不幸的是,我无法使用CursorAdapter,并且我必须请求整个数据库并将结果存储在对象模型中,但是结果非常快,因此可以.

Thanks to @pskink, I've now a fast single request to retrieve what I want. Unfortunately, I cannot use the CursorAdapter and I've to request the whole DB and store result in an object model, but the result is very fast so it's OK.

这是完整的代码:

public ArrayList<ContactModel> retrieveContactList(){

    ArrayList<ContactModel> list = new ArrayList<>();
    LongSparseArray<ContactModel> array = new LongSparseArray<>();

    Set<String> set = new HashSet<String>();
    set.add(ContactsContract.Data.MIMETYPE);
    set.add(ContactsContract.Data.CONTACT_ID);
    set.add(ContactsContract.Data.PHOTO_ID);
    set.add(ContactsContract.CommonDataKinds.Phone.NUMBER);
    set.add(ContactsContract.CommonDataKinds.Phone.TYPE);
    set.add(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    set.add(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
    set.add(ContactsContract.Contacts.PHOTO_ID);

    Uri uri = ContactsContract.Data.CONTENT_URI;
    String[] projection = set.toArray(new String[0]);
    String selection = ContactsContract.Data.MIMETYPE + " in (?, ?)";
    String[] selectionArgs = {
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
    };
    String sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE;

    Cursor cursor = this.context.getContentResolver().query(
        uri,
        projection,
        selection,
        selectionArgs,
        sortOrder
    );

    final int mimeTypeIdx = cursor.getColumnIndex(ContactsContract.Data.MIMETYPE);
    final int idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID);
    final int phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    final int phoneTypeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    final int givenNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    final int familyNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
    final int photoIdIdx = cursor.getColumnIndex(ContactsContract.Data.PHOTO_ID);

    while (cursor.moveToNext()) {

        long id = cursor.getLong(idIdx);
        ContactModel addressBookContact = array.get(id);

        if (addressBookContact == null) {
            addressBookContact = new ContactModel(id);
            array.put(id, addressBookContact);
            list.add(addressBookContact);
        }

        Long photoId = cursor.getLong(photoIdIdx);
        if (photoId != null){
            addressBookContact.addPhotoId(photoId);
        }

        switch (cursor.getString(mimeTypeIdx)) {
            case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                // row's data: see ContactsContract.CommonDataKinds.Phone
                addressBookContact.addPhone(cursor.getInt(phoneTypeIdx), cursor.getString(phoneIdx));
                break;
            case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                // row's data: see ContactsContract.CommonDataKinds.StructuredName
                addressBookContact.addName(cursor.getString(givenNameIdx), cursor.getString(familyNameIdx));
                break;
        }
    }

    cursor.close();

    return list;
}

这篇关于如何在单个请求中检索电话号码/带照片的身份证/名字/姓氏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆