gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:15 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com