活动中的代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_items, menu);
initSearchViewWidget(menu);
setSearchViewOnQueryTextListener();
setSearchViewOnSuggestionListener();
setSearchViewOnCloseListener();
return super.onCreateOptionsMenu(menu);
}
private void initSearchViewWidget(Menu menu){
MenuItem menuItemSearch = menu.findItem(R.id.globalSearchView);
searchView = (SearchView) menuItemSearch.getActionView();
}
private void setSearchViewOnQueryTextListener(){
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
startUsersSearchInMainFragment(query, false);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
startUsersSearchInMainFragment(newText, true);
filterFriendsListInCabinetFragment(newText);
return true;
}
});
}
private void setSearchViewOnSuggestionListener(){
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
CursorAdapter adapter = searchView.getSuggestionsAdapter();
Cursor cursor = adapter.getCursor();
if(cursor.moveToFirst()){
if(cursor.getPosition() == position){
String value = cursor.getString(cursor.getColumnIndex(UsersSearchAutocomplete.COLUMN_ALT));
searchView.setQuery(value, true);
cursor.moveToFirst();
}else{
if(cursor.move(position)){
String value = cursor.getString(cursor.getColumnIndex(UsersSearchAutocomplete.COLUMN_ALT));
searchView.setQuery(value, true);
cursor.moveToFirst();
}
}
}
return true;
}
@Override
public boolean onSuggestionClick(int position) {
CursorAdapter adapter = searchView.getSuggestionsAdapter();
Cursor cursor = adapter.getCursor();
if(cursor.moveToFirst()){
if(cursor.getPosition() == position){
String value = cursor.getString(cursor.getColumnIndex(UsersSearchAutocomplete.COLUMN_ALT));
searchView.setQuery(value, true);
cursor.moveToFirst();
}else{
if(cursor.move(position)){
String value = cursor.getString(cursor.getColumnIndex(UsersSearchAutocomplete.COLUMN_ALT));
searchView.setQuery(value, true);
cursor.moveToFirst();
}
}
}
return true;
}
});
}
private void setSearchViewOnCloseListener(){
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
reloadMainFragment();
return false;
}
});
}
private void startUsersSearchInMainFragment(String query, boolean autocomplete) {
if (fragment instanceof MainFragment) {
((MainFragment) fragment).cancelAsyncTasks();
((MainFragment) fragment).setFragmentDataWithQuery(query);
if (autocomplete) {
if (query.length() > 1) {
cancelAutocompleteAsyncTask();
usersSearchAutocomplete = new UsersSearchAutocomplete(MainActivity.this, query);
usersSearchAutocomplete.execute();
}
}
}
}
AsyncTask中的代码
@Override
protected void onPostExecute(String[] suggests) {
super.onPostExecute(suggests);
if(suggests != null){
if(suggests.length > 0){
String[] columnNames = {COLUMN_ID, COLUMN_ALT};
final MatrixCursor cursor = new MatrixCursor(columnNames);
for(int id = 0; id < suggests.length; id++){
String[] temp = new String[2];
temp[0] = String.valueOf(id);
temp[1] = suggests[id];
cursor.addRow(temp);
}
try{
Dialogs.createShortToast(activity.getApplicationContext(), Arrays.toString(suggests));
((MainActivity) activity).suggestCursor = null;
((MainActivity)activity).suggestCursor = cursor;
((MainActivity)activity).autocompleteAdapter = null;
((MainActivity)activity).autocompleteAdapter = new UsersSearchAutocompleteAdapter(activity.getApplicationContext(), ((MainActivity)activity).suggestCursor, 0);
((MainActivity)activity).getSearchView().setSuggestionsAdapter(null);
((MainActivity)activity).getSearchView().setSuggestionsAdapter(((MainActivity)activity).autocompleteAdapter);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
我的适配器
public class UsersSearchAutocompleteAdapter extends CursorAdapter {
public UsersSearchAutocompleteAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.search_autocomplete_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.searchAutocompleteTextView);
String alt = cursor.getString(cursor.getColumnIndexOrThrow("alt"));
textView.setText(alt);
}
}
我使用android.support.v7.widget.SearchView。建议在键入文本长度时开始搜索&gt; 1个符号。但是当我输入文字时,建议在searchview下显示并不是每一个都是。