(谷歌翻译)
简介: 我已经搜索了此解决方案,但没有找到它。如果此问题重复,请告诉我。谢谢。
问题: 我使用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();
}
答案 0 :(得分:0)
我的问题已经解决了:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// 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())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
谢谢