android - 使用 SUGGEST_COLUMN_ICON_1 的网址作为搜索建议

标签 android url uri search-suggestion android-searchmanager

我有一个 SearchManager 设置,其中建议下拉列表将在用户键入时显示。结果来 self 的服务器(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) 

我只有一个 URL。哪个选项最适合我?

这是我正在执行此操作的:

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;
    }
}

最佳答案

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

将其添加到您应用的 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'
}

现在让我们对问题中的代码进行一些更改(在 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);

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

毕竟你可以调用:

// do things with bitmap and then release when finished:
Glide.clear(futureTarget);

关于android - 使用 SUGGEST_COLUMN_ICON_1 的网址作为搜索建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32173898/

相关文章:

java - 仅加密图像文件的内容而不是整个文件

android - 如何在 DatePickerDialog 中将当前日期设置为 MinDate

android - 在 android for windows 上运行 kivy

Python:使用 Bottle 框架从浏览器检索当前 URL

javascript - NODEJS、DROPBOX 重定向链接

java - 使用 wicket 通过 AJAX 更改 URL

ios - 如何定义以 http ios7 开头的 url 方案

Android - 隐藏 API clientId 和 clientSecret 的最佳方式

ruby-on-rails - Rails 4-request.original_fullpath和request.fullpath有什么区别

swift - 如何使用 Swift/AWSAppSync 从临时目录获取 localUri