问题描述
我在寻找一些信息关于延长SearchRecentSuggestionsProvider,这样它会处理两种类型的建议:
I'm looking for some information on extending SearchRecentSuggestionsProvider such that it will handle two types of suggestions:
- 用户的搜索历史记录
- predefined,我想总是尽可能的建议条款
例如,当用户开始输入咕,也许他们已经搜索了咕飘之后 - 也许我也想加入谷歌,因为它在我的特殊条款名单。
For example, when a user starts typing "Goo", maybe they've searched for "Goo Gone" before - and maybe I'd also like to include "Google" since it's in my list of "special terms".
在我见我真的可以做一个非此即彼的 - 使用谷歌的code或推出自己的SQLite数据库来存储的东西。有没有办法覆盖SearchRecentSuggestionsProvider的一部分做我想要什么?
Referring to the documentation at Google I see that I can really do an either or - use Google's code or roll my own SQLite database to store things. Is there a way to override part of SearchRecentSuggestionsProvider to do what I want?
推荐答案
是的,你可以用自己的类扩展SearchRecentSuggestionsProvider。这是我写这样做的类:https://github.com/bostonbusmap/bostonbusmap/blob/master/src/boston/Bus/Map/provider/TransitContentProvider.java
Yes, you can extend SearchRecentSuggestionsProvider with your own class. This is the class I wrote to do this: https://github.com/bostonbusmap/bostonbusmap/blob/master/src/boston/Bus/Map/provider/TransitContentProvider.java
package boston.Bus.Map.provider;
import boston.Bus.Map.data.RoutePool;
import boston.Bus.Map.main.Main;
import boston.Bus.Map.provider.DatabaseContentProvider.DatabaseAgent;
import boston.Bus.Map.transit.TransitSystem;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.SearchRecentSuggestionsProvider;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
public class TransitContentProvider extends SearchRecentSuggestionsProvider {
private UriMatcher matcher;
public static final String AUTHORITY = "com.bostonbusmap.transitprovider";
public static final int MODE = SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES;
private static final int SUGGESTIONS_CODE = 5;
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
public TransitContentProvider()
{
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SUGGESTIONS_CODE);
setupSuggestions(AUTHORITY, MODE);
}
@Override
public boolean onCreate() {
boolean create = super.onCreate();
return create;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
int code = matcher.match(uri);
switch (code)
{
case SUGGESTIONS_CODE:
if (selectionArgs == null || selectionArgs.length == 0 || selectionArgs[0].trim().length() == 0)
{
return super.query(uri, projection, selection, selectionArgs, sortOrder);
}
else
{
ContentResolver resolver = getContext().getContentResolver();
return DatabaseAgent.getCursorForSearch(resolver, selectionArgs[0]);
}
default:
return super.query(uri, projection, selection, selectionArgs, sortOrder);
}
}
}
这篇关于使用SearchRecentSuggestionsProvider一些predefined条款?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!