How to implement an autocomplete search in the action bar using an HTTP request?

I added a search widget to my action bar and would like to handle the autocomplete function. After writing more than three letters, he should execute an http request to my web API, which will return a json result and should display suggestions of search widgets. But in the documentation there is a case with content providers. How can I organize the autofill function?

Added search in the xml file menu:

<item android:id="@+id/search" android:icon="@drawable/ic_search_white_24dp" android:title="Search" [namespace]:showAsAction="always" [namespace]:actionViewClass="android.widget.SearchView" /> 

Matches search customizable configuration using SearchView:

 public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.navigation, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); return super.onCreateOptionsMenu(menu); } 

Search feature added:

 <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/search_hint" android:searchSuggestAuthority="com.my.domain.searchable_activity" /> 

And ultimately added an empty responsible activity.

+7
android search autocomplete
source share
1 answer

You cannot do this with setSearchableInfo() and the search configuration.

The problem is that SearchView requires a CursorAdapter , and you are CursorAdapter data from the server, not from the database.

However, I did something similar before using the following steps:

  • Customize your SearchView to use the CursorAdapter ;

      searchView.setSuggestionsAdapter(new SimpleCursorAdapter( context, android.R.layout.simple_list_item_1, null, new String[] { SearchManager.SUGGEST_COLUMN_TEXT_1 }, new int[] { android.R.id.text1 })); 
  • Create an AsyncTask to read JSON data from your server and create a MatrixCursor from the data:

     public class FetchSearchTermSuggestionsTask extends AsyncTask<String, Void, Cursor> { private static final String[] sAutocompleteColNames = new String[] { BaseColumns._ID, // necessary for adapter SearchManager.SUGGEST_COLUMN_TEXT_1 // the full search term }; @Override protected Cursor doInBackground(String... params) { MatrixCursor cursor = new MatrixCursor(sAutocompleteColNames); // get your search terms from the server here, ex: JSONArray terms = remoteService.getTerms(params[0]); // parse your search terms into the MatrixCursor for (int index = 0; index < terms.length(); index++) { String term = terms.getString(index); Object[] row = new Object[] { index, term }; cursor.addRow(row); } return cursor; } @Override protected void onPostExecute(Cursor result) { searchView.getSuggestionsAdapter().changeCursor(result); } } 
  • Set OnQueryTextListener to start a remote server task or start a search:

      searchView.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextChange(String query) { if (query.length() >= SEARCH_QUERY_THRESHOLD) { new FetchSearchTermSuggestionsTask().execute(query); } else { searchView.getSuggestionsAdapter().changeCursor(null); } return true; } @Override public boolean onQueryTextSubmit(String query) { // if user presses enter, do default search, ex: if (query.length() >= SEARCH_QUERY_THRESHOLD) { Intent intent = new Intent(MainActivity.this, SearchableActivity.class); intent.setAction(Intent.ACTION_SEARCH); intent.putExtra(SearchManager.QUERY, query); startActivity(intent); searchView.getSuggestionsAdapter().changeCursor(null); return true; } } }); 
  • Set OnSuggestionListener to SearchView to search:

      searchView.setOnSuggestionListener(new OnSuggestionListener() { @Override public boolean onSuggestionSelect(int position) { Cursor cursor = (Cursor) searchView.getSuggestionsAdapter().getItem(position); String term = cursor.getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); cursor.close(); Intent intent = new Intent(MainActivity.this, SearchableActivity.class); intent.setAction(Intent.ACTION_SEARCH); intent.putExtra(SearchManager.QUERY, term); startActivity(intent); return true; } @Override public boolean onSuggestionClick(int position) { return onSuggestionSelect(position); } }); 
+14
source

All Articles