1

i am using firebase ui to populate data from the firebase firestore backend, and what i want to do is perform a filter or sort operation. so that means i want to update the query object that is passed to the recyclerviewqueryoptions object which is then passed to the adapter at runtime.

@Override
public void onFilter(Filters filters) {
   mFireStore = FirebaseFirestore.getInstance();
     Query query = mFireStore.collection("Products");

    // Category (equality filter)
    if (filters.hasCategory()) {
        query = query.whereEqualTo(Product.FIELD_CATEGORY, filters.getCategory());
    }

    // City (equality filter)
    if (filters.hasCity()) {
        query = query.whereEqualTo(Product.FIELD_CITY, filters.getCity());
    }

    // Price (equality filter)
    if (filters.hasPrice()) {
        query = query.whereEqualTo(Product.FIELD_PRICE, filters.getPrice());
    }
    // Sort by (orderBy with direction)
    if (filters.hasSortBy()) {
        query = query.orderBy(filters.getSortBy(), filters.getSortDirection());
    }

    // Limit items
    query = query.limit(10);


  //  db = FirebaseFirestore.getInstance();

    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    FirestoreRecyclerOptions<Product> response = new FirestoreRecyclerOptions.Builder<Product>()
            .setQuery(query,Product.class)
            .build();


    adapter = new FirestoreRecyclerAdapter<Product,ExploreViewholder>(response) {

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

        @Override
        protected void onBindViewHolder(@NonNull ExploreViewholder holder, int position, @NonNull Product model) {
            holder.productName.setText(model.getProductName());
            holder.creatorName.setText(mUser.getDisplayName());
            holder.productDesc.setText(model.getProductDescription());
            Glide.with(getContext())
                    .load(model.getProductImageUrl())
                    .into(holder.productImage);

        }
    };

    adapter.notifyDataSetChanged();
    mViewModel.setFilters(filters);



}

How can i update the data the recyclerview is going to display with the new query. or is this done automatically.. the query object above is going to get new data at runtime, theres no set query method i can call.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
caleb grimah
  • 183
  • 1
  • 15
  • There is no way to change the query that an adapter uses, nor is it possible to modify a Query after it's been created. See https://stackoverflow.com/questions/40919636/how-to-update-a-firebaserecycleradapters-query – Frank van Puffelen Feb 08 '18 at 04:16
  • Thanks..so i am concluding there are no drawbacks to creating an adapter for each of the new queries – caleb grimah Feb 08 '18 at 05:15
  • Also on a different note, how would you suggest i send data from a dialog fragment to the fragment that called it..i'm trying to implement a filters dialog but when i open the dialog from a fragment and select the filters and apply, i dont get the data in the fragment.note..the fragment that calls the dialog fragment is in a viewpager in the mainactivity... – caleb grimah Feb 08 '18 at 05:25
  • You can create a custom Adapter class to accomplish this but its not fully optimal. See: https://stackoverflow.com/a/53235783/683658 – Marco RS Nov 10 '18 at 03:38

0 Answers0