我想用搜索过滤一些数据的ArrayList,
在我的活动中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();
}
}
}
过滤器由于某些原因在我键入内容时清除了recyclelerview
答案 0 :(得分:3)
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();
}
在通知之前更改主列表 !!
branchArrayList = (ArrayList<Branch>) results.values;
将此行添加到publishResults
您已使用过滤结果
完成 NOTHING
答案 1 :(得分:0)
尝试将适配器更改为
public class BranchAdapter extends RecyclerView.Adapter<BranchAdapter.ViewHolder> implements Filterable {
private ArrayList<Branch> mArrayList;
private ArrayList<Branch> mFilteredList;
private CustomFilter filter;
public BranchAdapter(ArrayList<Branch> items) {
mArrayList = items;
mFilteredList = items;
filter = new CustomFilter(BranchAdapter.this);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_branch, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.data = branchArrayList.get(position);
holder.branch.setText(String.valueOf(mArrayList.get(position).getBranchCode() + " / " + mArrayList.get(position).getBranchName()));
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
@Override
public Filter getFilter() {
return filter;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View view;
public final TextView branch;
public Branch data;
public ViewHolder(View view) {
super(view);
this.view = view;
branch = view.findViewById(R.id.textView_branch);
}
}
public class CustomFilter extends Filter {
private BranchAdapter adapter;
private CustomFilter(BranchAdapter adapter) {
super();
this.adapter = adapter;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = mArrayList;
} else {
ArrayList<Branch> filteredList = new ArrayList<>();
for (Branch branch : mArrayList) {
if (branch.getBranchName().toLowerCase().contains(charString)) {
filteredList.add(branch);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredList = (ArrayList<Branch>) filterResults.values;
notifyDataSetChanged();
// this.adapter.notifyDataSetChanged();
}
}
}
答案 2 :(得分:0)
branchArrayList
之前,更新publishResults method
中的notifyDataSetChanged
答案 3 :(得分:0)
使用以下代码
更新您的PublishResults()
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredBranchArrayList = (ArrayList<Branch>) filterResults.values;
notifyDataSetChanged();
}