Show selection from suggestion list in Android searchview

后端 未结 1 754
无人共我
无人共我 2021-02-13 18:18

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.

I

相关标签:
1条回答
  • 2021-02-13 18:51

    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;
    }
    
    0 讨论(0)
提交回复
热议问题