在这段代码中,我搜索了多个文本ex。我有搜寻sachin。
sachin是显示列表,但单击以退回sachin到sac不会显示到适配器上的sa列表。没有任何库该如何解决?
当我按退格键并删除字符时,这次的dos not list不显示。
例
假设我已搜索sachin到文本,将sachin更改为sac。在这种情况下,不显示sa列表。我已经清除了所有列表,这种情况下会显示所有列表。
搜索显示列表
萨钦和萨尔达
现在搜索囊
现在显示萨钦
只搜索囊到囊
那时只显示sachin
不显示清单中的sardar
我的适配器
private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Build> buildlist = new ArrayList<Build>();
for (int i = 0; i < buildList.size(); i++) {
if ((buildList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Build babydata = new Build(buildList.get(i).getImages(), buildList.get(i).getName());
buildlist.add(babydata);
}
}
results.count = buildlist.size();
results.values = buildlist;
} else {
results.count = buildList.size();
results.values = buildList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
buildList = (ArrayList<Build>) results.values;
notifyDataSetChanged();
}
}
Activity 课
EditText editTxt = (EditText) findViewById(R.id.search);
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() <= 0) {
Log.d("jay", "s.length() <= 0" + s.toString());
buildList.clear();
buildList.addAll(buildListCopy);
recyclerView.setAdapter(null);
buildCustomAdapter = new BuildCustomAdapter(buildList);
recyclerView.setAdapter(buildCustomAdapter);
} else {
buildCustomAdapter.getFilter().filter(s.toString());
}
Log.d("jay", "mobisharnam" + s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
最佳答案
没有任何库简单逻辑
最后我在这个问题上有解决方案
在您的适配器构造器中添加新的副本列表
复制列表添加到过滤器后
private List<YourModel> modelList;
private List<YourModel> modelListCopy;
private ItemFilter mFilter = new ItemFilter();
public YourAdapter(List<YourModel> modelList) {
this.modelList= modelList;
this.modelListCopy= new ArrayList<>();
modelListCopy.addAll(modelList);
}
private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<YourModel> filterList = new ArrayList<YourModel>();
for (int i = 0; i < modelListCopy.size(); i++) {
if ((modelListCopy.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
YourModel builddata = new YourModel(modelListCopy.get(i).getImages(), modelListCopy.get(i).getName());
filterList.add(builddata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = modelListCopy.size();
results.values = modelListCopy;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
modelList= (ArrayList<YourModel>) results.values;
notifyDataSetChanged();
}
Activity 课
像这样
CustomAdapter youradapter= new CustomAdapter(modelList);