如何从Firebase在RecyclerView中添加搜索过滤器

时间:2019-01-18 05:03:15

标签: android firebase firebase-realtime-database filter android-recyclerview

我曾经从事过有关如何按值在FireBase中进行搜索的搜索工作,但我没有完整的答案,所以我现在要问

我使用了这种方法,但无法获得正确的结果:

public class Search extends Fragment {

public Search() {
    // Required empty public constructor
}


private SearchView mSearch;
private RecyclerView rvSearch;
private DatabaseReference mRef;
private SearchPlaceAdapter mAdapter;
ArrayList<Place> arrayList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_search, container, false);


    //Recylerview of place
    rvSearch = (RecyclerView) view.findViewById(R.id.rvSearch);
    rvSearch.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    //final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    //layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rvSearch.setLayoutManager(layoutManager);

    mRef = FirebaseDatabase.getInstance().getReference();

    mRef.child("Place").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            arrayList = new ArrayList<>();
            for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
                arrayList.add(eventSnapshot.getValue(Place.class));
            }

            mAdapter = new SearchPlaceAdapter(getContext(), arrayList);
            rvSearch.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });


    //searchView of place
    mSearch = (SearchView) view.findViewById(R.id.mSearch);
    mSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if(mAdapter!=null){
                mAdapter.getFilter().filter(newText);
            }
            return false;
        }
    });

    return view;
}

}

适配器代码 ................................................... ....

public class SearchPlaceAdapter extends RecyclerView.Adapter<SearchPlaceAdapter.SearchPlaceAdapterViewHolder> implements Filterable {

    Context mCntx;
    public ArrayList<Place> arrayList;
    public ArrayList<Place> arrayListFiltered;

    public SearchPlaceAdapter(Context mCntx, ArrayList<Place> arrayList)
    {
        this.mCntx = mCntx;
        this.arrayList = arrayList;
        arrayListFiltered = arrayList;
        /*arrayListFiltered = new ArrayList<>(arrayList);*/
    }

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

    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public SearchPlaceAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_layout, parent, false);

        SearchPlaceAdapterViewHolder viewHolder = new SearchPlaceAdapterViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(SearchPlaceAdapterViewHolder holder, final int position)
    {
        final Place place = arrayList.get(position);

        holder.txtPlace.setText(arrayList.get(position).getBuilding());

        Picasso.with(mCntx).load(place.getPlaceImg()).into(holder.image);//using picasso to load image

        holder.cardPlace.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(mCntx, CardviewSearch.class);
                intent.putExtra("placeId", String.valueOf(place.getPlaceId()));
                intent.putExtra("building", String.valueOf(place.getBuilding()));
                intent.putExtra("street", String.valueOf(place.getStreet()));
                intent.putExtra("imgurl", String.valueOf(place.getPlaceImg()));
                mCntx.startActivity(intent);

            }
        });
    }


    public class SearchPlaceAdapterViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtPlace;
        ImageView image;
        CardView cardPlace;

        public SearchPlaceAdapterViewHolder(View itemView) {
            super(itemView);
            txtPlace = (TextView) itemView.findViewById(R.id.txtPlace);
            image = (ImageView) itemView.findViewById(R.id.image);
            cardPlace = (CardView) itemView.findViewById(R.id.cardPlace);
        }
    }

    public Filter getFilter()
    {
        return exampleFilter;
    }

    private Filter exampleFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Place> filteredList = new ArrayList<>();

            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(arrayListFull);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();

                for (Place item : arrayListFull) {
                    if (item.getBuilding().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }

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

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults results) {
            arrayList.clear();
            arrayList.addAll((List)results.values);
            notifyDataSetChanged();
        }
    };


}

它仅通过前置关键字搜索,而不过滤整个内容。如何使用包含?

我希望用所有可能的值搜索值,而不仅仅是前值。请我帮忙。

1 个答案:

答案 0 :(得分:0)

希望有帮助:

public class SearchPlaceAdapter extends RecyclerView.Adapter<SearchPlaceAdapter.SearchPlaceAdapterViewHolder> implements Filterable {

    Context mCntx;
    public ArrayList<Place> arrayList;
    public ArrayList<Place> arrayListFiltered;

    public SearchPlaceAdapter(Context mCntx, ArrayList<Place> arrayList)
    {
        this.mCntx = mCntx;
        this.arrayList = arrayList;
        this.arrayListFiltered = new ArrayList<>(arrayList);
    }

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

    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public SearchPlaceAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_layout, parent, false);

        SearchPlaceAdapterViewHolder viewHolder = new SearchPlaceAdapterViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(SearchPlaceAdapterViewHolder holder, final int position)
    {
        final Place place = arrayList.get(position);

        holder.txtPlace.setText(arrayList.get(position).getBuilding());

        Picasso.with(mCntx).load(place.getPlaceImg()).into(holder.image);//using picasso to load image

        holder.cardPlace.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(mCntx, CardviewSearch.class);
                intent.putExtra("placeId", String.valueOf(place.getPlaceId()));
                intent.putExtra("building", String.valueOf(place.getBuilding()));
                intent.putExtra("street", String.valueOf(place.getStreet()));
                intent.putExtra("imgurl", String.valueOf(place.getPlaceImg()));
                mCntx.startActivity(intent);

            }
        });
    }


    public class SearchPlaceAdapterViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtPlace;
        ImageView image;
        CardView cardPlace;

        public SearchPlaceAdapterViewHolder(View itemView) {
            super(itemView);
            txtPlace = (TextView) itemView.findViewById(R.id.txtPlace);
            image = (ImageView) itemView.findViewById(R.id.image);
            cardPlace = (CardView) itemView.findViewById(R.id.cardPlace);
        }
    }

    public Filter getFilter()
    {
        return exampleFilter;
    }

    private Filter exampleFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Place> filteredList = new ArrayList<>();

            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(arrayListFiltered);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();

                for (Place item : arrayListFiltered) {
                    if (item.getBuilding().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }

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

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults results) {
            arrayList.clear();
            arrayList.addAll((List)results.values);
            notifyDataSetChanged();
        }
    };
}