#java #android
Использую android.support.v7.widget.SearchView. Сделал автоподстановку значений.
Почему-то подстановка не работает, список автоподстановки просто не появляется если
пользователь ввел только один символ. Я так понимаю, должно быть какое-то свойство
типа SetThreshold, но не могу его найти.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), expandListener);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() == 0)
return false;
Cursor cursor = DB.getInstance(this).getSuggestions(newText);
if (cursor.getCount() > 0) {
String[] columns = new String[] { "_id", "type", "source" };
int[] columnTextId = new int[] { R.id.ivTypeIcon, R.id.tvType, R.id.tvSearchResult };
searchAutoComplete = new SuggestionSimpleCursorAdapter(getBaseContext(),
R.layout.search_result_item, cursor,
columns, columnTextId,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
searchView.setSuggestionsAdapter(searchAutoComplete);
return true;
} else {
searchView.setSuggestionsAdapter(null);
cursor.close();
}
return false;
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
public class SuggestionSimpleCursorAdapter extends SimpleCursorAdapter
{
Context context;
int layout;
public SuggestionSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(this.context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(this.layout, parent, false);
return view;
}
@Override
public void bindView(View view, Context _context, Cursor _cursor) {
// my code
}
}
Обновление
После отладки удалось выяснить, что в адаптере не вызываются методы getCount, newView,
bindView по сути выполняется только конструктор. Хотя все данные валидны, в курсоре
данные есть.
Ответы
Ответ 1
Собственно решил проблему В searchable.xml обязательны теги label и hint после этого конфигурируем SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName()); searchView.setSearchableInfo(searchableInfo);
Комментариев нет:
Отправить комментарий