RecyclerView with Firebase Search display all products when searched

Ritesh Sharma :

When I enter some text in the search bar my program displays all of the products and not what I searched. Suppose if I enter 's' in my search bar it should only display products where their name starts with or contains 's' in it but it just displays all of the product. I can't figure out whats wrong with the code. So, Please help out.

This is my function where I am getting my data using RecyclerAdapter and then passing them onto the "UserViewHolder" class. Here I am trying to sort my data by using "orderbychild" on child 'pname' with firebaseSearchQuery.

Firebase Database.

products firebase database

    private void firebaseUserSearch(String searchText){

    Query firebaseSearchQuery = ProductRef.orderByChild("pname").startAt(searchText).endAt(searchText 
    + "\uf8ff");

    FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(ProductRef,Products.class).build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull 
        Products model) {

            holder.setDetails(getApplicationContext(), model.getPname(), model.getPprice(), 
        model.getPmrp(), model.getPcondition(), model.getPimage());

        }

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

    recyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

    }

UserViewHolder

   public static class UsersViewHolder extends RecyclerView.ViewHolder{

    View mView;

    public UsersViewHolder(@NonNull View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setDetails(Context context, String phoneName, String phonePrice, String phoneMrp, String phoneCondition, String phoneImage){

        TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
        TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
        TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
        TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
        ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);

        phone_name.setText(phoneName);
        phone_price.setText(phonePrice);
        phone_mrp.setText(phoneMrp);
        phone_condition.setText(phoneCondition);
        Picasso.with(context).load(phoneImage).into(phone_image);

    }
}
Peter Haddad :

Change this:

   FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(ProductRef,Products.class).build();

into this:

   FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(firebaseSearchQuery,Products.class).build();

Use the query firebaseSearchQuery in the setQuery() method

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349213&siteId=1