使用SUGGEST_COLUMN_ICON_1的网址进行搜索建议

The*_*ter 8 url android uri android-searchmanager search-suggestion

我有一个SearchManager设置,其中一个建议下拉列表将显示为用户键入.结果来自我的服务器(http).我想在每个选项中显示一个图标(如果文件确实存在).

查看文档,我看到常量列的选项SUGGEST_COLUMN_ICON_1允许这些选项:

Column name for suggestions cursor. Optional. If your cursor includes this column, then all suggestions will be provided in a format that includes space for two small icons, one at the left and one at the right of each suggestion. The data in the column must be a resource ID of a drawable, or a URI in one of the following formats:

content (SCHEME_CONTENT)
android.resource (SCHEME_ANDROID_RESOURCE)
file (SCHEME_FILE) 
Run Code Online (Sandbox Code Playgroud)

我只有一个URL.哪种选择最适合我?

这是class我在做这个的地方:

public class MyCustomSuggestionProvider extends SearchRecentSuggestionsProvider {

    public static final String AUTHORITY = "---.MyCustomSuggestionProvider";
    public static final int MODE = DATABASE_MODE_QUERIES;
    private final static String[] COLUMN_NAMES = {BaseColumns._ID,
            SearchManager.SUGGEST_COLUMN_TEXT_1,
            SearchManager.SUGGEST_COLUMN_TEXT_2,
            SearchManager.SUGGEST_COLUMN_QUERY,
            SearchManager.SUGGEST_COLUMN_INTENT_DATA,
            SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
            SearchManager.SUGGEST_COLUMN_ICON_1,
            SearchManager.SUGGEST_COLUMN_INTENT_ACTION};

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

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        Cursor recentCursor = super.query(uri, projection, selection,
                selectionArgs, sortOrder);

        String query = selectionArgs[0];
        if (query == null || query.length() < 3) {
            return recentCursor;
        }

        final MatrixCursor customCursor = new MatrixCursor(COLUMN_NAMES);

        // Get web results from Retrofit Library
        List<TheProfile> suggestions = RestClient.get().getCustomSearch(query, MyApp.getUserId());

        for (TheProfile suggestion : suggestions) {


            Uri searchIconUri = Uri.parse("http:/---/profile_images/" + String.valueOf(suggestion.id) + ".png");
            try {
                customCursor.addRow(new Object[]{
                        suggestion.id, suggestion.profile, suggestion.subcategory, suggestion.profile, suggestion.profile, suggestion.subcategory, searchIconUri, "android.intent.action.SEARCH"});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return customCursor;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*gey 4

对于那些像我一样仍在寻找这个问题答案的人。它与我的代码非常相似,所以我决定分享它。我花了几个小时把这一切放在一起。也许,我会为某人节省一些时间。首先,您需要Glide 库

将其添加到应用程序的 build.gradle 文件中:

repositories {
    mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:19.1.0'
}
Run Code Online (Sandbox Code Playgroud)

现在让我们对问题中的代码(在MyCustomSuggestionProvider课堂上)进行一些更改:将其放入您的for (TheProfile suggestion : suggestions) {

FutureTarget<File> futureTarget  = Glide
        .with(getContext().getApplicationContext())
        .load(searchIcon)
        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);

File cacheFile = futureTarget.get();
Uri searchIconUri = Uri.fromFile(cacheFile);
Run Code Online (Sandbox Code Playgroud)

注意这行代码: .with(getContext().getApplicationContext()) 这对于获取应用程序上下文非常重要,而不仅仅是上下文,因为我们不会在 ImageView 中显示 bmp。有关此类 Glide 用法的官方 Glide 文档。

毕竟你可以打电话:

// do things with bitmap and then release when finished:
Glide.clear(futureTarget);
Run Code Online (Sandbox Code Playgroud)