2015-11-05 73 views
1

我正在瀏覽Google Android Developer網站上的基本教程Retrieving a List of Contacts科:匹配任何類型排序CONTENT_FILTER_URI結果

我已經設置好了我的列表視圖,它顯示了預期的搜索結果,除了我想按顯示名稱(Contacts.DISPLAY_NAME_PRIMARY)排序。目前這只是一個看似隨意的混亂。

有問題的代碼如下:

@Override 
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) { 
    /* 
    * Appends the search string to the base URI. Always 
    * encode search strings to ensure they're in proper 
    * format. 
    */ 
    Uri contentUri = Uri.withAppendedPath(
      Contacts.CONTENT_FILTER_URI, 
      Uri.encode(mSearchString)); 
    // Starts the query 
    return new CursorLoader(
      getActivity(), 
      contentUri, 
      PROJECTION, 
      null, 
      null, 
      Contacts.DISPLAY_NAME_PRIMARY // <- SORT THIS, PLEASE! 
    ); 
} 

當我使用Contacts.CONTENT_URI我的contentUri顯示所有聯繫人,排序工作正常預期。

爲什麼它不適用於過濾結果?

回答

0

好吧,我設法回答我自己的問題!

望着例如谷歌的代碼,我設法找到這一行:

final static String SORT_ORDER = 
       Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME; 

所以,我需要在設備上使用Contacts.SORT_KEY_PRIMARY>蜂窩, 和Contacts.DISPLAY_NAME上的設備<蜂窩。

那些一些示例代碼,不明白:

@Override 
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) { 
    /* 
    * Appends the search string to the base URI. Always 
    * encode search strings to ensure they're in proper 
    * format. 
    */ 
    Uri contentUri = Uri.withAppendedPath(
      Contacts.CONTENT_FILTER_URI, 
      Uri.encode(mSearchString)); 

    String sortOrder; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    { 
     sortOrder = Contacts.SORT_KEY_PRIMARY; 
    } 
    else 
    { 
     sortOrder = Contacts.DISPLAY_NAME; 
    } 

    // Starts the query 
    return new CursorLoader(
      getActivity(), 
      contentUri, 
      PROJECTION, 
      null, 
      null, 
      sortOrder // Sorted by name 
    ); 
} 

我測試了它和它的作品。希望這可以幫助其他人。