Custom Suggestions in Search with custom layout
I have successfully implemented Custom Search suggestions when using the Searc开发者_JAVA百科h 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.
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;
}
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.
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.
精彩评论