如何使用http请求在操作栏中实现search视图自动完成?

我已经将search视图小部件添加到我的操作栏,并希望处理自动完成function。 写了更多的3个字母后,它应履行对我的Web API的HTTP请求,这将返回JSON结果,并应显示search小部件的build议。 但在文档中观察与内容提供者的情况。 如何组织自动填充function?

在菜单xml文件中添加了search视图:

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

将searchconfiguration与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的configuration:

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

并最终添加空的负责任的活动。

Solutions Collecting From Web of "如何使用http请求在操作栏中实现search视图自动完成?"

你不能用setSearchableInfo()和一个searchconfiguration来做到这一点。

问题是, SearchView需要一个CursorAdapter ,你正在从服务器,而不是数据库检索数据。

不过,我之前也是这样做了这样的事情:

  • 设置您的SearchView使用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 })); 
  • 创build一个AsyncTask来从服务器读取JSON数据并从数据创build一个MatrixCursor

     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); } } 
  • 设置一个OnQueryTextListener启动您的远程服务器任务或开始您的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; } } }); 
  • SearchView上设置一个OnSuggestionListener来执行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); } });