在此代码中,我搜索多个文本ex。我有搜索sachin。
当我按回空格并删除一个字符时,此时不显示不显示。 实施例
假设我搜索sachin文本将sachin更改为sac。在这种情况下,不显示sa列表。我清楚所有列表,案例显示所有列表。
搜索sa显示列表
萨钦和萨达尔
现在搜索囊
现在展示萨钦
只搜索sac到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();
}
}
活动类
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) {
}
});
答案 0 :(得分:2)
没有任何库简单的逻辑
最后我在这个问题上有解决方案
在适配器构造函数中添加新的副本列表
在过滤器中添加复制列表
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();
}
活动类
CustomAdapter youradapter= new CustomAdapter(modelList);
答案 1 :(得分:0)
我更喜欢谓词搜索过滤器 我已将您的代码更改为使用谓词,您必须使用google guava库 在你的gradle中包括以下依赖
compile 'com.google.guava:guava:19.0'
现在,请检查以下代码
EditText editTxt = (EditText) findViewById(R.id.search);
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String searchQuery = s.toString();
if (buildListCopy != null && buildListCopy.size() > 0) {
buildList = Lists.newArrayList(Collections2.filter(buildListCopy, new SearchFilter(searchQuery)));
recyclerView.setAdapter(null);
if (buildList != null && buildList.size() > 0) {);
buildCustomAdapter = new BuildCustomAdapter(buildList);
recyclerView.setAdapter(buildCustomAdapter);
recyclerView.getAdapter().notifyDataSetChanged();
}
}
return false;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
以下是我们上面使用的搜索过滤器谓词
/**
* Custom Predicate class to filter list as per search query
*/
public final class SearchFilter implements Predicate<Build> {
private final Pattern pattern;
public SearchFilter(final String regex) {
pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.LITERAL);
}
@Override
public boolean apply(final Build input) {
return pattern.matcher(input.getName().toLowerCase()).find();
}
}