我有一个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)

我所拥有的只是一个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;
    }
}

最佳答案

对于那些像我一样仍在寻找该问题答案的人。它与我的代码非常相似,因此我决定共享它。我花了几个小时将所有内容放在一起。也许,我会为某人节省一些时间。
首先,您需要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的网络URL获得搜索建议,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32173898/

10-11 20:51