gpt4 book ai didi

java - RecyclerView 搜索过滤器不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:23 26 4
gpt4 key购买 nike

我想通过搜索过滤一些 ArrayList 的数据,

在我的 Activity 的 onCreate 中:

        arrayList = getListItemData();
filteredArrayList = new ArrayList<>();
filteredArrayList.addAll(arrayList);

adapter = new NameAdapter(filteredArrayList);
itemList.setAdapter(adapter);

searchBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

adapter.getFilter().filter(s.toString());

}

@Override
public void afterTextChanged(Editable s) {

}
});

我的可过滤适配器:

public class NameAdapter extends RecyclerView.Adapter<NameAdapter.ViewHolder> implements Filterable {
private ArrayList<Name> arrayList;
private CustomFilter filter;

public NameAdapter(ArrayList<Branch> items) {
arrayList = items;
filter = new CustomFilter(NameAdapter.this);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_name, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.data = arrayList.get(position);


}

@Override
public int getItemCount() {
return branchArrayList.size();
}

@Override
public Filter getFilter() {
return filter;
}

public class ViewHolder extends RecyclerView.ViewHolder {
public final View view;
public final TextView branch;
public Name data;

public ViewHolder(View view) {
super(view);
this.view = view;
branch = view.findViewById(R.id.textView_name);

}

}

public class CustomFilter extends Filter {
private NameAdapter adapter;

private CustomFilter(NameAdapter adapter) {
super();
this.adapter = adapter;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredArrayList.clear();
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
filteredArrayList.addAll(arrayList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Name name : arrayList) {
if (name.getName().toLowerCase().startsWith(filterPattern)) {
filteredBranchArrayList.add(name);
}
}
}
results.values = filteredArrayList;
results.count = filteredArrayList.size();
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {

this.adapter.notifyDataSetChanged();
}
}
}

过滤器由于某种原因不起作用,当我输入内容时它会清除 recyclerview

最佳答案

1st 在构造函数中复制 branchArrayList。像这样:-

 private ArrayList<Branch> branchCopy = new ArrayList<>;
public BranchAdapter(ArrayList<Branch> items) {
branchArrayList = items;
branchCopy.addAll(items);
filter = new CustomFilter(BranchAdapter.this);
}

你的performingFilter

  @Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<Branch> branchFilter = new ArrayList<>;
final FilterResults results = new FilterResults();
if (constraint.length() == 0) {
branchFilter.addAll(branchArrayList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final Branch branch : branchCopy) {
if (branch.getBranchName().toLowerCase().startsWith(filterPattern)) {
branchFilter.add(branch);
}
}
}
results.values = branchFilter ;
results.count = branchFilter.size();
return results;
}

您的publishResults

  @Override
protected void publishResults(CharSequence constraint, FilterResults results) {
branchArrayList = (ArrayList<Branch>) results.values; // you have done nothing with the filter results
notifyDataSetChanged();
}

在通知更改 mainList 之前 !!

 branchArrayList = (ArrayList<Branch>) results.values;

将此行添加publishResults

You have done NOTHING with the filter results

关于java - RecyclerView 搜索过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49293686/

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