How to implement search view autocomplete in actionbar using http request?

后端 未结 1 1106
梦毁少年i
梦毁少年i 2021-02-03 10:19

I\'ve added search view widget to my action bar and would like to handle autocomplete feature. After writing more then 3 letters it should fulfill http request to my web API whi

1条回答
  •  臣服心动
    2021-02-03 11:03

    You can't do this with setSearchableInfo() and a search configuration.

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

    However, I have done something like this before with these steps:

    • Set up your SearchView to use a 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 the JSON data from your server and create a MatrixCursor from the data:

      public class FetchSearchTermSuggestionsTask extends AsyncTask {
      
          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 an OnQueryTextListener to kick off your remote server task or start your search activity:

          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 an OnSuggestionListener on the SearchView to execute your 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);
              }
          });
      

    0 讨论(0)
提交回复
热议问题