gpt4 book ai didi

java - 如何使用 Filter 比较原始列表与 recyclerview 中的过滤列表?

转载 作者:太空狗 更新时间:2023-10-29 13:11:50 25 4
gpt4 key购买 nike

在这段代码中,我搜索了多个文本。我有搜索引擎。

sachin 显示列表,但单击返回 sachin 到 sac 不显示到适配器上的 sa 列表。没有库怎么解决?


当我按退格键并删除一个字符时,这次不列出不显示。示例

假设我有搜索 sachin 到文本更改 sachin 到 sac。在这种情况下不显示 sa 列表。我已经清除所有列表,案例显示所有列表。
搜索sa显示列表
sachin 和 sardar
现在搜索sac
现在显示sachin
只搜索sac to sc
那时候只显示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);

关于java - 如何使用 Filter 比较原始列表与 recyclerview 中的过滤列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com