Всем привет! Возникла необходимость использовать фильтрацию списка (поиск в списке по значению в поле name), построенном с помощью SimpleAdapter. Подскажите, пожалуйста, как это можно реализовать и насколько возможно? Гугл оказался достаточно скуп на туториалы с использованием именно с помощью SimpleAdapter. В текущей реализации получается неверная фильтрация списка.
MainActivity class
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("Search here");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
Adapter class
public class MyAdapterMain extends SimpleAdapter {
private ArrayList<HashMap<String, String>> results;
private Context context;
public MyAdapterMain(Context context,ArrayList<HashMap<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
this.context = context;
}
public View getView(int position, View view, ViewGroup parent) {
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_currencies_main, null);
}
TextView id = (TextView) v.findViewById(R.id.id);
id.setText(results.get(position).get("id"));
TextView symbol = (TextView) v.findViewById(R.id.symbol);
symbol.setText(results.get(position).get("symbol"));
TextView name = (TextView) v.findViewById(R.id.name);
name.setText(results.get(position).get("name"));
TextView price = (TextView) v.findViewById(R.id.price);
price.setText(results.get(position).get("price"));
TextView percent_change_1h = (TextView) v.findViewById(R.id.percent_change_1h);
percent_change_1h.setText(results.get(position).get("percent_change_1h"));
TextView percent_change_24h = (TextView) v.findViewById(R.id.percent_change_24h);
percent_change_24h.setText(results.get(position).get("percent_change_24h"));
TextView percent_change_7d = (TextView) v.findViewById(R.id.percent_change_7d);
percent_change_7d.setText(results.get(position).get("percent_change_7d"));
TextView imgLink = (TextView) v.findViewById(R.id.imgLink);
ImageView img = (ImageView) v.findViewById(R.id.img);
imgLink.setText(results.get(position).get("imgLink"));
Picasso.with(context).load(results.get(position).get("imgLink")).into(img);
return v;
}
}
SimpleAdaptera
фильтр ищет соответствия в каждом слове всех значений по всем ключамHashMap
. Можно переопределить методgetFilter()
и возвращать свою реализацию фильтра, которую в свою очередь можно скопировать из сорцов адаптера и изменить под свои нужды. – woesss 22 мая '18 в 8:42SimpleFilter
- его он и использует для фильтрации. Но мне кажется Вам будет проще сделать по примеру: ru.stackoverflow.com/a/831779/11515 – woesss 22 мая '18 в 18:14