2016-08-11 4 views
7

사용자 연락처를 통해 실시간 검색을 구현하려고합니다. 일치하는 각 연락처의 이름, 미리보기 이미지 및 주소 (있는 경우)를 얻고 싶습니다.하나의 검색어로 주소가있는 주소록 (FORMATTED_ADDRESS)을 검색하는 방법은 무엇입니까?

사용자가 입력하는 동안 실시간 검색이 실행 중입니다.

그래서 그는 종류의 엄마와 '마틴', '매튜'얻을 것이다 ...

그는 내가 달성하려고

매트를 계속하겠습니다 만 '매튜'를 볼 수 있습니다 이것은 다음과 같은 단일 쿼리를 사용하지만 항상 연락처 번호는 FORMATTED_ADRESS 필드에 있습니다. 같은 질문에 ContactsContract.CommonDataKindsContactsContract.Contacts을 사용하고 있기 때문에 JOIN 문제가 있다고 생각하십니까?

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.DISPLAY_NAME 다음

    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); 
    
  • 하지만 같은 Contacts._ID와 두 번째 쿼리를 시작

    , Contacts._ID를 쿼리

    1. 으로, 내 문제를 해결 이것은 별표가 붙을 것이다. 두 번째 쿼리 모든 연락처, 이는 최선의 방법이 아닐 수도 있습니다.

      그래서 마지막 질문은 다음과 같습니다. 첫 번째 쿼리에서이 작업을 수행 할 수 있습니까?

    +0

    왜 downvote? 나는 놓친다? 뭔가? – longilong

    답변

    0
    Mmmh은 매우 슬픈, 아무도 내 질문에 대답하고 현상금 포인트 ;-(여기에 기록을 위해

    ,의 내 작업 예를 잡을 수 없다는 것을.이 문제를 해결하지만, 난 여전히 그것이 생산 생각

    모든 사용자 항목 (afterTextchange)에서 나는 먼저 자신의 이름 (cursor)에 질의를 포함하는 ID를 가진 모든 사용자를 얻고, 이후 모든 사용자가 주소를 얻으려고 다른 쿼리 (detailCursor)를 시작하는 getContactsDetailsQuery을 호출합니다. 과부하, 나는 한도를 더했다 ..

    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, selection, 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; 
    }