android – 如何使用一个查询搜索带地址的联系人(FORMATTED_ADDRESS)?

前端之家收集整理的这篇文章主要介绍了android – 如何使用一个查询搜索带地址的联系人(FORMATTED_ADDRESS)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试对用户联系人实施实时搜索,我想获取每个匹配联系人的姓名,缩略图和地址(如果有).

用户键入时正在运行实时搜索.

所以他输入了ma并将获得’martin’,’matthews’……

他将继续垫子,只会看到’matthews’

我尝试使用如下的单个查询来实现此目的,但我始终在FORMATTED_ADRESS字段中获取联系号码.我想我有一个JOIN问题,因为我在同一个查询中使用ContactsContract.CommonDataKinds和ContactsContract.Contacts?

public static List<ContactModel> getContactsForQuery(Context context,String query) {

    String[] projection = new String[] {
        ContactsContract.Contacts.DISPLAY_NAME,Contacts.PHOTO_THUMBNAIL_URI,ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
    };

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + query +  "%'";
    Cursor cursor = context.getContentResolver().query(uri,projection,selection,null,null);
    if (cursor.moveToFirst()) {

        do {
            String name = cursor.getString(0);
            String thumbail = cursor.getString(1);
            String formattedADress = cursor.getString(2);
        }
        while (cursor.moveToNext());
    }

我实际上解决了我的问题

>查询Contacts._ID,Contacts.DISPLAY_NAME
>使用Contacts._ID开始第二个查询,如下所示

Cursor detailCursor = context.getContentResolver().query(
       ContactsContract.Data.CONTENT_URI,new String[]{
           CommonDataKinds.StructuredPostal.STREET,CommonDataKinds.StructuredPostal.CITY,CommonDataKinds.StructuredPostal.POSTCODE
       },ContactsContract.Data.CONTACT_ID + "=? AND " 
           + CommonDataKinds.StructuredPostal.MIMETYPE + "=?",new String[]{
           String.valueOf(contactID),CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
       },null);

但这将为每个联系人启动第二个查询,这可能不是最好的方法.

所以我的最后一个问题是:是否可以使用第一个查询来完成这项工作?

解决方法

嗯,非常难过,没有人能够回答我的问题并抓住赏金点;-(

为了记录,这是我的工作示例.它解决了这个问题,但我仍然认为它会产生很大的负荷.在每个用户条目(afterTextchange)上,我调用getContactsDetailsQuery,它首先获取所有用户的ID,其ID包含其名称(光标)中的查询,然后我为每个用户启动另一个查询(detailCursor)以获取地址.为了防止过载,我增加了一个限制..

public static List<SearchModel> getContactDetailsForQuery(Context context,String query,int limit) {

    final int CONTACT_ID_INDEX = 0;
    final int CONTACT_NAME_INDEX = 1;
    final int CONTACT_THUMBNAIL_INDEX = 2;

    //my custom model to hold my results
    List<SearchModel> results = new ArrayList<SearchModel>();

    final String[] selectUser = new String[]{
        Contacts._ID,Contacts.DISPLAY_NAME,Contacts.PHOTO_THUMBNAIL_URI};
    String selection = Contacts.DISPLAY_NAME + " LIKE ?";
    String[] selectionArgs = new String[]{"%" + query + "%"};
    String sortOrder = Contacts.DISPLAY_NAME + " ASC";

    Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI,selectUser,selectionArgs,sortOrder,null);

    int contactCounter = 0;
    if (cursor != null && cursor.moveToFirst()) {

        do {
            String contactID = cursor.getString(CONTACT_ID_INDEX);
            String displayName = cursor.getString(CONTACT_NAME_INDEX);
            String thumbnail = cursor.getString(CONTACT_THUMBNAIL_INDEX);

            //get user details with user id (this is the query i wanted to change in my question!!)
            Cursor detailCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[]{
                    CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},ContactsContract.Data.CONTACT_ID + "=? AND " +
                    CommonDataKinds.StructuredPostal.MIMETYPE + "=?",new String[]{String.valueOf(contactID),CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},null);

            if (detailCursor != null && detailCursor.moveToFirst()) {
                //special case: user has several address,query all of them
                do {
                    String formattedAddress = detailCursor.getString(detailCursor.getColumnIndex(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                    //user has serveral adress -> init model for each adress
                    SearchModel contact = new SearchModel();
                    results.add(contact);
                    contactCounter++;

                } while (detailCursor.moveToNext() && contactCounter < limit);

            } else {

                //user has no adress -> init model
                SearchModel contact = new SearchModel();
                results.add(contact);
                contactCounter++;
            }

            detailCursor.close();

        } while (cursor.moveToNext() && contactCounter < limit);
    }
    cursor.close();

    return results;
}
原文链接:https://www.f2er.com/android/315146.html

猜你在找的Android相关文章