使用 SearchRecentSuggestionsProvider 和一些预定义的术语?

问题描述 投票:0回答:1

我正在寻找一些有关扩展 SearchRecentSuggestionsProvider 的信息,以便它将处理两种类型的建议:

android search autocomplete
1个回答
1
投票

是的,您可以使用自己的类扩展 SearchRecentSuggestionsProvider。这是我为此编写的类: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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.