具有自定义布局的搜索中的自定义建议

发布于 2024-10-24 03:06:25 字数 284 浏览 2 评论 0原文

我在使用搜索对话框时成功实现了自定义搜索建议。然而目前每个建议项目只是文本。我想在左侧添加一个图标。但问题是我本地没有这些图片,而是可以通过 URL 访问它们。我无法在文档中找到( Building建议表)如何将 URL 而不是 Uri 发送到 SUGGEST_COLUMN_ICON_1 下的本地文件。

I have successfully implemented Custom Search suggestions when using the Search Dialog. However currently each suggestions item is just text. I would like to add in a icon on the left. The problem however is that I do not have these pictures locally, rather I can access them via a URL. I haven't been able to find in the documents ( Building a suggestion table ) how to send it the URL instead of a Uri to a local file under SUGGEST_COLUMN_ICON_1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2024-10-31 03:06:25

所以这是可能的,但这是相当愚蠢和有点hacky的。您必须创建一个 ContentProvider 来提供应用程序内部缓存目录中的图像(请参阅我的答案https://stackoverflow.com/ a/15667914/473201)。

然后,由于您的文件 ContentProvider 将在主 UI 线程上访问,因此您无法在那里下载图像。您必须在搜索建议 ContentProvider 的 query() 函数中下载所有图像。如果您将它们放在应用程序的内部缓存目录中,则当 SearchView 请求它们时,文件 ContentProvider 可以返回它们。

像这样:

class ImagesProvider extends ContentProvider {
...
// stuff from https://stackoverflow.com/a/15667914/473201
...
}

然后在您的搜索 ContentProvider 中:

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

   // create a cursor table of results
   MatrixCursor mc = new MatrixCursor(columns);
   for (SearchResult res : results) {

      String filename = res.getId();
      ImagesProvider.downloadAndSaveFile(filename, res.getPictureUrl());

      mc.addRow(new Object[] {
        res.getId(),
        res.getName(),
        ContentUris.withAppendedId(ImagesProvider.CONTENT_URI, filename)
      });
   }
   return mc;
}

So this is possible, but it's quite a doozy and kind of hacky. You have to create a ContentProvider to serve up the images from your app's internal cache directory (see my answer here https://stackoverflow.com/a/15667914/473201 ).

Then, since your file ContentProvider is going to be accessed on the main UI thread, you can't download the images there. You have to download all the images in the query() function of your search suggestions ContentProvider. If you put them in your app's internal cache dir, the file ContentProvider can return them when the SearchView asks for them.

Something like:

class ImagesProvider extends ContentProvider {
...
// stuff from https://stackoverflow.com/a/15667914/473201
...
}

Then in your search ContentProvider:

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

   // create a cursor table of results
   MatrixCursor mc = new MatrixCursor(columns);
   for (SearchResult res : results) {

      String filename = res.getId();
      ImagesProvider.downloadAndSaveFile(filename, res.getPictureUrl());

      mc.addRow(new Object[] {
        res.getId(),
        res.getName(),
        ContentUris.withAppendedId(ImagesProvider.CONTENT_URI, filename)
      });
   }
   return mc;
}
感情旳空白 2024-10-31 03:06:25

我发现这是不可能的,只要你想使用默认的Android搜索功能。如果您知道其他情况,请评论。

I have figured out that this is impossible, as long as you want to use the default Android search capabilities. If you know otherwise, please comment.

物价感观 2024-10-31 03:06:25

我知道这是一个老问题,但 StackOverflow 关于该主题的帖子似乎很少。

我花了很多时间(大约 5 个小时)尝试让搜索使用自定义布局(希望结果全屏显示并按类别分段,这是一个相当自定义的布局),并使用 android 文档 关于这个主题,但是我的 SearchActivity (及其定义的使用 SearchWidget 时从未创建自定义布局,因为 Android 系统直接调用我的内容提供程序的查询方法并在丑陋的下拉列表中显示结果。我最终滚动了自己的自定义搜索,花了大约 1 小时,所以简短的答案是,滚动你自己的。

I know this is an old question, but StackOverflow posts on the topic seem sparse.

I spent a good amount of time (~5 hours) trying to get search to work with a custom layout (wanted results to be full screen and segmented by category, it is quite a custom layout) and using the methodology described in the android documentation on the subject, however my SearchActivity (with its defined custom layout) was never being created when using the SearchWidget as the Android system was calling my Content Provider's query method directly and presenting results in an ugly dropdown list. I ended up rolling my own custom search and it took ~1 hour, so short answer is, roll your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文