Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show selection from suggestion list in Android searchview

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);
}
}
like image 449
dschulten Avatar asked Jan 29 '14 06:01

dschulten


People also ask

How do I get text from searchView?

To get text of SearchView , use searchView. getQuery() .

How do I set text in searchView?

You can use setQuery() to change the text in the textbox. However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked. To fix this problem, just call searchView.


1 Answers

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;
}
like image 77
dschulten Avatar answered Oct 01 '22 01:10

dschulten