0

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
  • 78,874
  • 25
  • 140
  • 134

1 Answers1

1

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • thanks it works and how i can i make it Not Case Sensitive? – Ritesh Sharma Mar 23 '20 at 09:24
  • you can do this https://stackoverflow.com/questions/37643459/case-insensitive-sorting-with-firebase-orderbychild, or insert the data lowercase – Peter Haddad Mar 23 '20 at 09:26
  • what i want to achieve is that when a user enters text in searchbar it should display that product whether user enters the name in caps or small letters. – Ritesh Sharma Mar 23 '20 at 09:31
  • check this https://stackoverflow.com/questions/49995926/how-to-search-firebase-text-by-lower-case-also and this https://stackoverflow.com/questions/38590937/firebase-query-methods-startat-taking-case-sensitive-parameters (basically either store two versions or just store everything lowercase and when retrieving u can change first letter to uppercase) – Peter Haddad Mar 23 '20 at 09:33
  • https://stackoverflow.com/questions/28589092/firebase-query-find-item-with-child-that-contains-string – Peter Haddad Mar 23 '20 at 10:06