使用SearchView过滤FirestoreRecyclerA

使用SearchView过滤FirestoreRecyclerA

本文介绍了如何使用SearchView过滤FirestoreRecyclerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerAdapter.java

public class IncidentsRecyclerAdapter extends FirestoreRecyclerAdapter<Incidents, IncidentsRecyclerAdapter.IncidentsHolder> implements Filterable {
    private OnItemClickListener listener;
    private ObservableSnapshotArray<Incidents> list;
    private ArrayList<Incidents> listFull;

    public IncidentsRecyclerAdapter(@NonNull FirestoreRecyclerOptions<Incidents> options) {
        super(options);
        this.list = options.getSnapshots();
        this.listFull = new ArrayList<>(options.getSnapshots());
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindViewHolder(@NonNull IncidentsHolder holder, int position, @NonNull Incidents model) {
        Picasso.get()
                .load(model.getIncident_image_one())
                .placeholder(R.mipmap.ic_launcher)
                .fit()
                .centerCrop()
                .into(holder.imageViewIncident);

        holder.textViewName.setText(model.getUser_name());
        holder.textViewDateTime.setText(model.getIncident_timestamp());
        holder.textViewDescription.setText(model.getIncident_location() + "/" + model.getIncident_desc());
    }

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

    @Override

    public Filter getFilter() {
        return dataFilter;
    }

    private Filter dataFilter = new Filter() {
        @Override
        // Will be executed in a background thread
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Incidents> filteredList = new ArrayList<>();

            if(constraint == null || constraint.length() == 0) {
                filteredList.addAll(listFull);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();
                for (Incidents item : listFull) {
                    if(item.toString().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list.clear();
            list.addAll((Collection<? extends Incidents>) results.values);
            notifyDataSetChanged();
        }
    };
}

每当在工具栏上按下搜索图标时,RecyclerView就会消失,并且搜索过滤器将自动不起作用.

Whenever the search icon is pressed on the toolbar, the RecyclerView vanishes and automatically the Search FIlter does not work.

我认为将 Firestorecycleroptions 投射到 ArrayList 时做错了.

I think I have done something wrong while casting Firestorecycleroptions to ArrayList.

更新
根据此链接,Firebase不支持对文本-领域.另外,尝试一种变通方法,可以将 FirestoreRecyclerAdapter 数据转换为原始 String List Array 类型以便可以执行本地搜索.Algolia/ElastiSeacrch可能是一个负担.请指导此更新是否错误或不合逻辑

UPDATE
As per this link, Firebase does not support full-text search on text-fields. Also, trying for a workaround where I can cast the FirestoreRecyclerAdapter data to primitive String, List, or Array type so that a local search can be implemented. Algolia/ElastiSeacrch might be a burden to handle.Please, guide if this update was wrong or illogical

推荐答案

只需从构造函数中删除Firestorerecycleroptions,仅使用列表而不调用super():

Just remove Firestorerecycleroptions from the constructor and use only your list without call super():

public IncidentsRecyclerAdapter(List<Incidents> list) {
    this.list = list;
    this.listFull = new ArrayList<>(list);
}

这篇关于如何使用SearchView过滤FirestoreRecyclerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:20