使用搜索视图过滤 recyclerview 中的 ListItem 不起作用

Filtering ListItem in recyclerview not working using search view

我想使用 searchview

过滤从 json 列表中获取的结果

我试过使用 Filter getFilter() 方法,但似乎不起作用。

这是我的适配器class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements Filterable {



    public List<ListItem> listItems;
    public Context context;
    public List<ListItem> AllBanks;



    public MyAdapter(List<ListItem> listItemAdap, Context context) {
        this.listItems = listItemAdap;
        this.context = context;
        this.AllBanks = new ArrayList<>(listItemAdap);



    }

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


    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);
        holder.txtviewBankName.setText(listItem.getBank_name());
        holder.txtviewAddress.setText(listItem.getAddress());
        holder.txtviewIfsc.setText(listItem.getIfsc());
        holder.txtviewBranch.setText(listItem.getBranch());
        holder.txtviewBankId.setText(listItem.getBank_id());
        holder.txtviewCity.setText(listItem.getCity());
        holder.txtviewDistrict.setText(listItem.getDistrict());
        holder.txtviewState.setText(listItem.getState());



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

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




    private Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            List<ListItem> filteredList = new ArrayList<>();

            if (charSequence.toString().isEmpty())
            {
                filteredList.addAll(AllBanks);
            }
            else
            {
                for (ListItem Banks: AllBanks)
                {
                    if ( Banks.toString().toLowerCase().contains(charSequence.toString().toLowerCase()))
                    {
                        filteredList.add(Banks);
                    }
                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;

            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            listItems.clear();
            listItems.addAll((Collection<? extends ListItem>) filterResults.values);
            notifyDataSetChanged();

        }
    };

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView txtviewBankName;
        public TextView txtviewAddress;
        public TextView txtviewIfsc;
        public TextView txtviewBranch;
        public TextView txtviewBankId;
        public TextView txtviewCity;
        public TextView txtviewDistrict;
        public TextView txtviewState;

        public ViewHolder(View itemView) {
            super(itemView);

            txtviewBankName = (TextView)itemView.findViewById(R.id.bankName);
            txtviewAddress = (TextView)itemView.findViewById(R.id.address);
            txtviewIfsc = (TextView)itemView.findViewById(R.id.ifsc);
            txtviewBranch = (TextView)itemView.findViewById(R.id.branch);
            txtviewBankId = (TextView)itemView.findViewById(R.id.bankId);
            txtviewCity = (TextView)itemView.findViewById(R.id.city);
            txtviewDistrict = (TextView)itemView.findViewById(R.id.district);
            txtviewState = (TextView)itemView.findViewById(R.id.state);
        }
    }
}

这是我的 ListItem class

public class ListItem {

private String bank_name;
private String address;
private String ifsc;
private String branch;
private String bank_id;
private String city;
private String district;
private String state;

public ListItem(String bank_name, String address, String ifsc, String branch, String bank_id, String city, String district, String state)
{
    this.bank_name = bank_name;
    this.address = address;
    this.ifsc = ifsc;
    this.branch = branch;
    this.bank_id = bank_id;
    this.city = city;
    this.district = district;
    this.state = state;
}

public String getBank_name()
{
    return bank_name;
}

public String getAddress() {
    return address;
}

public String getIfsc() {
    return ifsc;
}

public String getBranch() {
    return branch;
}

public String getBank_id() {
    return bank_id;
}

public String getCity() {
    return city;
}

public String getDistrict() {
    return district;
}

public String getState() {
    return state;
}

}

我希望在我开始在搜索字段中输入时过滤获取的列表视图。

也许 onCreateOptionsMenu 和 Filter getFilter 部分可以帮助这个网站

http://programmingroot.com/android-recyclerview-search-filter-tutorial-with-example/