0

This is a snippet of the code, I am creating the adapter in the call to my livedata observe method.

 mViewModel.getMutableLiveData().observe(this, new Observer<Filters>() {
            @Override
            public void onChanged(@Nullable Filters filters) {
                if(filters != null){

                   FirebaseFirestore  mFireStored = FirebaseFirestore.getInstance();

                    Log.d("FiltersINfoVM",filters.getPrice() +" ");
                    Log.d("FiltersINfoVM",filters.getSortBy() +" ");
                    Log.d("FiltersINfoVW",filters.getCategory() +" ");
                    Log.d("FiltersINfoVW",filters.getSearchDescription(getContext()) +" ");
                    Log.d("FiltersINfoVW",filters.getCity() +" ");
                    // Construct query basic query
                    CollectionReference collectionReference = mFireStored.collection("Products");
                    //Query query = collectionReference.orderBy("Products");
                    // Category (equality filter)
            //        if (filters.hasCategory()) {
            //            query2 = collectionReference.whereEqualTo(Product.FIELD_CATEGORY,"docs" );//filters.getCategory()
            //        }

                                //     City (equality filter)
            //        if (filters.hasCity()) {
            //            query2 = collectionReference.whereEqualTo(Product.FIELD_CITY,"owerri" );//filters.getCity()
            //        }
            //
            //             //    Price (equality filter)
            //        if (filters.hasPrice()) {
            //            query2 = collectionReference.whereEqualTo(Product.FIELD_PRICE, filters.getPrice());
            //        }
                    // Sort by (orderBy with direction)
//                    if (filters.hasSortBy()) {
//                        //query = collectionReference.orderBy(filters.getSortBy(), filters.getSortDirection())
//                        Log.d("Sort Values method",filters.getSortBy());
//                        query2 = collectionReference.orderBy(filters.getSortBy(),filters.getSortDirection());
//                    }
                    query2 = collectionReference.orderBy("productName", Query.Direction.ASCENDING);

                    // Limit items
                    //query = collectionReference.limit(LIMIT);
                    // query = collectionReference.orderBy("numRatings", Query.Direction.DESCENDING);

                    FirestoreRecyclerOptions<Product> response2 = new FirestoreRecyclerOptions.Builder<Product>()
                            .setQuery(query2,Product.class)
                            .build();

                    FirestoreRecyclerAdapter<Product,ExploreViewholder>
                            adapterResp = new FirestoreRecyclerAdapter<Product,ExploreViewholder>(response2) {

                        @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);

                        }

                        @Override
                        public void onError(FirebaseFirestoreException e) {
                            Log.e("error", e.getMessage());
                        }
                    };

                    adapterResp.notifyDataSetChanged();
                   // mRecycler.setAdapter(adapterResp);
                    mRecycler.swapAdapter(adapterResp,true);
                }    
            }
        });

I am getting data in the filters object in the onchanged method, and everything works as expected except when it is time for the overridden methods of the Firestore recycler adapter to get called,(they dont, i set breakpoints and tried debugging, execution just skips those methods), as a result the RecyclerView do not display any data. "Product" is a model class and ExploreViewHolder is the viewholder for the RecyclerView. The problem is the first time I query the database I receive data and is displayed in the recycler view..but I then want to query the data again at runtime, I am doing this using a dialog fragment, I create a new query with a new adapter and I swap the old adapter for this one..the problem is It does not display anything in the recyclerview after this new query...

caleb grimah
  • 183
  • 1
  • 15
  • Have you put a layout manager on your recyclerView? – karandeep singh Feb 17 '18 at 08:41
  • Yes..i have..or do I have to apply another layout manager when I set a new adapter? – caleb grimah Feb 17 '18 at 09:53
  • you should call try calling `notifyDataSetChanged` after `swapAdapter`. Also, any particular reason for using `swapAdapter` and not `setAdapter`? – karandeep singh Feb 17 '18 at 10:00
  • I am using swapadapter because I am using two adapters and this is the second one, I think the documentation advices I use it because I want to recycle the same viewholder from the last adapter... – caleb grimah Feb 17 '18 at 10:17
  • Possible duplicate of [If there is data in query or Path it show data in FirestoreRecyclerAdapter. but when there is no data at that path](https://stackoverflow.com/questions/48808417/if-there-is-data-in-query-or-path-it-show-data-in-firestorerecycleradapter-but) – Alex Mamo Feb 17 '18 at 12:59
  • See the answer from the duplicate question. Don't forget to use: `adapter.startListening();` and `adapter.stopListening();`. I'm sure you haven't used those lines of code. – Alex Mamo Feb 17 '18 at 13:01
  • I am using the startlistening and stoplistening method in onstart and onstop.. – caleb grimah Feb 17 '18 at 13:06
  • You might use it but on wrong object. I cannot see your `FirestoreRecyclerAdapter adapterResp` to be declared as global. – Alex Mamo Feb 17 '18 at 14:33
  • Omg...thanks alot..i ws calling start and stop listening on the wrong object...you were right alex mamo..thanks again..its working now – caleb grimah Feb 17 '18 at 16:34
  • https://stackoverflow.com/questions/47228262/android-listview-adapter-not-pushing-individual-items-for-chat-app-firebase-ui/47228433#47228433 Also this – Peter Haddad Feb 21 '18 at 14:52

0 Answers0