Hunt
Hunt

Reputation: 8425

set text in searchView after selecting item from searchable suggestion window

I have implemented searchable suggestion in my android application and its show the result in autocomplete text ( i have bind with a search view ).

Following is my configuration

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" />

here are my matcher

 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);

Now the issues is when I type text and see the autocomplete list and taps over any item inside the autocomplete , its opening the same activity, rather then setting the selected item in a searchview. I don't know how to resolve this issue.

Upvotes: 0

Views: 9202

Answers (2)

Gokul Kulkarni
Gokul Kulkarni

Reputation: 2239

You can try the following it will work,

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;
        }
    });

Don't forget to return "true" in the onSuggestionClick callback, if you return true it makes sure that the event is not returned to the parent, but if you return false, android assumes you want even the parent to handle the event and the parent event handler gets called.

Upvotes: 8

Aibar Zhexenaliuly
Aibar Zhexenaliuly

Reputation: 21

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;
        }
    });

Upvotes: 2

Related Questions