我在我的Android应用程序中实现了可搜索的建议,并在自动完成文本中显示结果(我已经与搜索视图绑定)。
以下是我的配置
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search Name"
android:searchSuggestAuthority="com.mdb.db"
android:searchSuggestIntentAction="android.intent.action.VIEW" />
这是我的匹配器
MATCHER.addURI(MyApp.AUTHORITY,SearchManager.SUGGEST_URI_PATH_QUERY, DB.NAME.SEARCH_SUGGEST);
MATCHER.addURI(MyApp.AUTHORITY,SearchManager.SUGGEST_URI_PATH_QUERY+"/*", DB.NAME.SEARCH_SUGGEST);
现在问题是当我输入文字并查看自动填充列表并点击自动填充内的任何项目时,它会打开相同的活动,而不是在searchview
中设置所选项目。我不知道如何解决这个问题。
答案 0 :(得分:6)
您可以尝试以下方法,
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
mSearchView.setQuery(/*get the clicked item here*/, false); //to set the text
return true;
}
});
不要忘记在onSuggestionClick回调中返回“true”,如果返回true,则确保事件不会返回到父级,但如果返回false,则android假定您甚至希望父级处理事件和父事件处理程序被调用。
答案 1 :(得分:0)
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
MatrixCursor temp = (MatrixCursor) mAdapter.getItem(position);
searchView.setQuery(temp.getString(1), true);
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
return true;
}
});