2012-07-04 76 views
8

我有一個搜索小部件(SearchView),在輸入時顯示自定義建議。我已按照official guide添加最近的查詢建議,但我仍然只有自定義建議。
對於每個搜索,我的片段調用這個函數(驗證):最近的搜索沒有顯示,自定義建議是

private void addQueryToRecent(String query) { 
    SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), 
      MyCustomSuggestionProvider.AUTHORITY, 
      MyCustomSuggestionProvider.MODE); 
    suggestions.saveRecentQuery(query, "recent"); 
} 

我的建議提供者似乎確定:

public class MyCustomSuggestionProvider extends SearchRecentSuggestionsProvider { 

public final static String AUTHORITY = "com.zgui.musicshaker.provider.MyCustomSuggestionProvider"; 
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES; 

public MyCustomSuggestionProvider() { 
    setupSuggestions(AUTHORITY, MODE); 
} 

@Override 
public Cursor query(Uri uri, String[] projection, String sel, 
     String[] selArgs, String sortOrder) { 
    //retrieves a custom suggestion cursor and returns it 
} 
} 

searchable.xml:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
android:hint="artist, track informations..." 
android:label="@string/app_name" 
android:queryAfterZeroResults="true" 
android:searchSuggestAuthority="com.zgui.musicshaker.provider.MyCustomSuggestionProvider" 
android:searchSuggestSelection=" ?" /> 

清單:

  <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
<provider 
     android:name=".provider.MyCustomSuggestionProvider" 
     android:authorities="com.zgui.musicshaker.provider.MyCustomSuggestionProvider" 
     android:enabled="true" > 
    </provider> 

Acording到that post,我應該是有數據/數據/ app.package.name /數據庫/ databasename.db最近的搜索分貝,但我似乎沒有...
或者,也許我是否應該在MyCustomSuggestionProvider.query()返回的遊標中自己添加「最近的搜索建議」?任何想法是值得歡迎...

+0

你沒有使用SearchSuggestionProvider,怎麼回事? –

回答

6

發現:目前還不清楚在所有Android上的文檔,但我需要讓自己的要求和MyCustomSuggestionProvider.query合併光標():

Cursor recentCursor = super.query(uri, projection, sel, selArgs, 
      sortOrder); 
Cursor[] cursors = new Cursor[] { recentCursor, customCursor}; 
return new MergeCursor(cursors); 

請確保您有相同的列在這兩個雖然...

+3

你是如何弄清楚如何匹配列的? – KickingLettuce

+0

你的customCursor是什麼樣的? –

+1

你是什麼意思,「確保你有兩個相同的列」? –

1
private static final String[] SEARCH_SUGGEST_COLUMNS = { 
     SearchManager.SUGGEST_COLUMN_FORMAT, 
     SearchManager.SUGGEST_COLUMN_ICON_1, 
     SearchManager.SUGGEST_COLUMN_TEXT_1, 
     SearchManager.SUGGEST_COLUMN_INTENT_DATA, 
     BaseColumns._ID }; 

以上是在最近的搜索建議DB使用的列的列表,使用相同的列表爲您定製的搜索建議來避免衝突

+1

我不認爲所有這些參數都是必需的。 –

1

爲了擴展elgui的回答,下面是我如何匹配了該列的例子:

@Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
     final Cursor recentsCursor = super.query(uri, projection, selection, selectionArgs, sortOrder); 
     final Cursor customResultsCursor = queryCache(recentsCursor, selectionArgs[0]); 
     return new MergeCursor(new Cursor[]{recentsCursor, customResultsCursor}); 
    } 

    private Cursor queryCache(Cursor recentsCursor, String userQuery) { 
     final MatrixCursor arrayCursor = new MatrixCursor(recentsCursor.getColumnNames()); 

     final int formatColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_FORMAT); 
     final int iconColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1); 
     final int displayColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1); 
     final int queryColumnIndex = recentsCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_QUERY); 
     final int idIndex = recentsCursor.getColumnIndex("_id"); 

     final int columnCount = recentsCursor.getColumnCount(); 

     // Populate data here 
     int startId = Integer.MAX_VALUE; 

     for (String customSearchResult : customSearchResults) { 
      final Object[] newRow = new Object[columnCount]; 
      if (formatColumnIndex >= 0) newRow[formatColumnIndex] = 0; 
      if (iconColumnIndex >= 0) newRow[iconColumnIndex] = R.drawable.invisible; 
      if (displayColumnIndex >= 0) newRow[displayColumnIndex] = customSearchResult; 
      if (queryColumnIndex >= 0) newRow[queryColumnIndex] = customSearchResult; 
      newRow[idIndex] = startId--; 
      arrayCursor.addRow(newRow); 
     }  

     return arrayCursor; 
    } 

由於這是基於SearchRecentSuggestionsProvider的實施細則,但仍可能無法在其他情況下,它可能是更有價值編寫你自己的最近歷史記錄並一起完成。

相關問題