一个月前,我将ActionBarSherlock 4.2放入我的项目中。除了我SearchView
的搜索建议外,我得到了一切。我创建搜索建议的方式是使用the method in the Android documentation。
ActionBarSherlock是否支持搜索建议?我试图在Github页面上挖掘the issue列表,但问题似乎已经结束但我似乎无法跟进讨论并了解它是否真的已经解决了。我认为你们中的一些人一直在使用ActionBarSherlock可能会更清楚。
答案 0 :(得分:12)
没有。但我找到了一种方法来让它查询你的ContentProvider。 我查看了API 17中的SuggestionsAdapter源代码,其中查询执行并得到了替换此方法的想法。另外我发现ActionbarSherlock的SuggestionsAdapter不使用你的SearchableInfo。
编辑ActionBarSherlock项目中的com.actionbarsherlock.widget.SuggestionsAdapter:
添加一行
private SearchableInfo searchable;
在构造函数中,添加
this.searchable = mSearchable;
将getSuggestions方法替换为以下方法:
public Cursor getSuggestions(String query, int limit) {
if (searchable == null) {
return null;
}
String authority = searchable.getSuggestAuthority();
if (authority == null) {
return null;
}
Uri.Builder uriBuilder = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel()
.fragment(""); // TODO: Remove, workaround for a bug in Uri.writeToParcel()
// if content path provided, insert it now
final String contentPath = searchable.getSuggestPath();
if (contentPath != null) {
uriBuilder.appendEncodedPath(contentPath);
}
// append standard suggestion query path
uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);
// get the query selection, may be null
String selection = searchable.getSuggestSelection();
// inject query, either as selection args or inline
String[] selArgs = null;
if (selection != null) { // use selection if provided
selArgs = new String[] { query };
} else { // no selection, use REST pattern
uriBuilder.appendPath(query);
}
if (limit > 0) {
uriBuilder.appendQueryParameter("limit", String.valueOf(limit));
}
Uri uri = uriBuilder.build();
// finally, make the query
return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
}
现在它查询我的ContentProvider但是崩溃了默认适配器,说没有layout_height从支持库加载一些xml文件。所以你必须使用自定义SuggestionsAdapter。这对我有用:
import com.actionbarsherlock.widget.SearchView;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public final class DrugsSearchAdapter extends CursorAdapter
{
private static final int QUERY_LIMIT = 50;
private LayoutInflater inflater;
private SearchView searchView;
private SearchableInfo searchable;
public DrugsSearchAdapter(Context context, SearchableInfo info, SearchView searchView)
{
super(context, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
this.searchable = info;
this.searchView = searchView;
this.inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View v, Context context, Cursor c)
{
String name = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
TextView namet = (TextView) v.findViewById(R.id.list_item_drug_name);
namet.setText(name);
String man = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
TextView manuf = (TextView) v.findViewById(R.id.list_item_drug_manufacturer);
manuf.setText(man);
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2)
{
return this.inflater.inflate(R.layout.list_item_drug_search, null);
}
/**
* Use the search suggestions provider to obtain a live cursor. This will be called
* in a worker thread, so it's OK if the query is slow (e.g. round trip for suggestions).
* The results will be processed in the UI thread and changeCursor() will be called.
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
String query = (constraint == null) ? "" : constraint.toString();
/**
* for in app search we show the progress spinner until the cursor is returned with
* the results.
*/
Cursor cursor = null;
if (searchView.getVisibility() != View.VISIBLE
|| searchView.getWindowVisibility() != View.VISIBLE) {
return null;
}
try {
cursor = getSuggestions(searchable, query, QUERY_LIMIT);
// trigger fill window so the spinner stays up until the results are copied over and
// closer to being ready
if (cursor != null) {
cursor.getCount();
return cursor;
}
} catch (RuntimeException e) {
}
// If cursor is null or an exception was thrown, stop the spinner and return null.
// changeCursor doesn't get called if cursor is null
return null;
}
public Cursor getSuggestions(SearchableInfo searchable, String query, int limit) {
if (searchable == null) {
return null;
}
String authority = searchable.getSuggestAuthority();
if (authority == null) {
return null;
}
Uri.Builder uriBuilder = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.query("")
.fragment("");
// if content path provided, insert it now
final String contentPath = searchable.getSuggestPath();
if (contentPath != null) {
uriBuilder.appendEncodedPath(contentPath);
}
// append standard suggestion query path
uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);
// get the query selection, may be null
String selection = searchable.getSuggestSelection();
// inject query, either as selection args or inline
String[] selArgs = null;
if (selection != null) { // use selection if provided
selArgs = new String[] { query };
} else { // no selection, use REST pattern
uriBuilder.appendPath(query);
}
if (limit > 0) {
uriBuilder.appendQueryParameter("limit", String.valueOf(limit));
}
Uri uri = uriBuilder.build();
// finally, make the query
return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
}
}
并在SearchView中设置此适配器
searchView.setSuggestionsAdapter(new DrugsSearchAdapter(this, searchManager.getSearchableInfo(getComponentName()), searchView));
答案 1 :(得分:2)
我是为此开放the github issue的人。它正在开发分支。当前版本(4.2)没有修复。它由commit完全修复,但我建议只检查dev分支并尝试它。
答案 2 :(得分:0)
我不知道我在这里是不是错了,或者我在事故中改变了一些东西,但上面的答案不起作用,ActionBarSherlock SuggestionsAdapter不起作用。我得到的只是runQueryOnBackgroundThread中的空指针。它也从未进入bindView等,但它设法显示建议结果。我认为android.app.SearchManager以某种方式用getSuggestions()重写ABS,但我不确定。我还在尝试......