从建议列表中的Andr​​oid搜索查看显示的选择 [英] Show selection from suggestion list in Android searchview

查看:186
本文介绍了从建议列表中的Andr​​oid搜索查看显示的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个建议列表中搜索查看。如果用户从列表中选择一个项目,一个新的意图被发送,我可以申请我的过滤器,但搜索视图是空的。

如果我更新onNewIntent搜索视图setQuery(见下文),其效果是,所选择的项目显示在搜索视图,但推荐列表弹出试。我能避免这一点,只显示在搜索视图中的当前查询的文本,而不建议列表弹出?

  @覆盖
保护无效onNewIntent(意向意图){
如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
    最终的查询字符串= intent.getStringExtra(SearchManager.QUERY);
    如果(!query.equals(searchView.getQuery())){
    sea​​rchView.setQuery(查询,假); //使建议弹出
    }
    在applyFilter(查询);
}
}
 

解决方案

的技巧是通过使用onSubmitListener在搜索视图,并从其onSuggestionClick方法返回true,而不是调用setQuery(更换搜索管理器的默认行为查询,假)在意向处理程序:

  @覆盖
公共布尔onSuggestionClick(INT位置){
字符串的建议= getSuggestion(位置);
sea​​rchView.setQuery(建议,真正的); //现在提交查询
返回true; //更换默认搜索管理器行为
}

私人字符串getSuggestion(INT位置){
光标光标=(光标)searchView.getSuggestionsAdapter()的getItem(
    位置);
字符串suggest1 = cursor.getString(光标
    .getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
返回suggest1;
}
 

I have a searchview with suggestion list. If the user selects an item from the list, a new intent is sent and I can apply my filter, but the search view remains empty.

If I update the search view in onNewIntent with setQuery (see below), the effect is that the selected item is shown in the search view, but the suggestion list pops up again. Can I avoid that and only show the current query text within the search view without the suggestion list popping up?

@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    final String query = intent.getStringExtra(SearchManager.QUERY);
    if (!query.equals(searchView.getQuery())) {
    searchView.setQuery(query, false); // makes the suggestions pop up
    }
    applyFilter(query);
}
}

解决方案

The trick is to replace the default behaviour of the search manager by using an onSubmitListener on the search view and returning true from its onSuggestionClick method, rather than calling setQuery(query, false) in the intent handler:

@Override
public boolean onSuggestionClick(int position) {
String suggestion = getSuggestion(position);
searchView.setQuery(suggestion, true); // submit query now
return true; // replace default search manager behaviour
}

private String getSuggestion(int position) {
Cursor cursor = (Cursor) searchView.getSuggestionsAdapter().getItem(
    position);
String suggest1 = cursor.getString(cursor
    .getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
return suggest1;
}

这篇关于从建议列表中的Andr​​oid搜索查看显示的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆