1

I am new to Android and just started to understand some concepts like the RecyclerView. I am using Firebase as a database so I implemented the Firebase solution for that.

My Adapter:

FirebaseRecyclerAdapter<Offer,OfferViewHolder> adapter = new FirebaseRecyclerAdapter<Offer, OfferViewHolder>(
            Offer.class,
            R.layout.card_item,
            OfferViewHolder.class,
            mDatabaseReference.child("offer").getRef()
    ) {
        @Override
        protected void populateViewHolder(OfferViewHolder viewHolder, Offer model, int position) {
            if(tvNoMovies.getVisibility()== View.VISIBLE){
                tvNoMovies.setVisibility(View.GONE);
            }

            viewHolder.tvHeading.setText(model.getHeader());
            viewHolder.tvStoreName.setText(model.getStoreName());

        }
    };

Now I have two questions:

  1. Is it possible to filter results inside the adapter or do i have to use ChildEventListeners for that?
  2. When referencing to a key that contains an Array or an object itself how to retrieve child values?
adjuremods
  • 2,938
  • 2
  • 12
  • 17
A. Ilazi
  • 341
  • 2
  • 21

1 Answers1

2

You can initialize the FirebaseRecyclerAdapter with either a DatabaseReference or a Query. As its name implies, the latter allows you to get a subset of the children at a specific location in the database.

For example say if you want to only show offers from a specific store:

DatabaseReference offers = mDatabaseReference.child("offer").getRef();
Query storeOffers = offers.orderByChild("storeName").equalTo("A. lazzi's store");
FirebaseRecyclerAdapter<Offer,OfferViewHolder> adapter = new FirebaseRecyclerAdapter<Offer, OfferViewHolder>(
        Offer.class,
        R.layout.card_item,
        OfferViewHolder.class,
        storeOffers
) {
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I already use the query function. What I want to achieve is filter first by "choosen cities" and then by "choosen categories". I know that I will get more data then needed but I cant think of a good solution to avoid that – A. Ilazi Nov 02 '16 at 01:10
  • Firebase queries can only filter by a single property. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Nov 02 '16 at 01:27
  • Thanks a lot. Right now I am using a childListener and filter the result in the events... I am going to do the udacity firebase course before I start to get on your nerves :D. – A. Ilazi Nov 02 '16 at 01:57