【发布时间】:2019-04-11 00:12:48
【问题描述】:
(谷歌翻译)
简介: 我已经搜索过这个解决方案,但没有找到。如果有此问题的重复,请告诉我。谢谢。
问题: 我使用 Room 数据库和 RecycleView 创建了一个存储应用程序。然后我寻找将搜索添加到我的数据的方法。一切都很顺利,但有一个问题。 如果减少搜索词,则搜索仅限于可见数据。 (看截图)
希望你能理解。 请帮我。我看过很多教程,但没有任何效果。
谢谢
我的代码: 在我的适配器
中过滤public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}
MainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;
adapter.getFilter().filter(newText);
return false;
}
我的适配器中也有这个:
void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}
【问题讨论】:
标签: android android-recyclerview searchview android-room android-filterable