0

I am trying to query Firebase and populate a recycler adapter with conditional data from the query's DataSnapshot. I tried putting the populate function inside the if statement that correctly logs the data I want, however the recycler view instead just returns everything from the node I was searching in (the main query I started with). Any suggestions on how to just populate the items that apply to the "if" statement? Thank you!

    rootRef = FirebaseDatabase.getInstance().getReference();
    //below is the node i query
    mAlbumQuery = rootRef.child(Constants.FIREBASE_CHILD_ALBUMS).orderByChild("genres");
    mAlbumQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot reco : dataSnapshot.getChildren()) {
                if (reco.getValue().toString().contains(mRecommendation.getGenre())) {
                    //below returns the items i want
                    Log.d("is this correct", reco.getValue().toString());
                    //below returns everything in the original query
                    //how to populate only items that match the above?
                    mAdapter = new FirebaseRecyclerAdapter<Album, AlbumsViewHolder>(
                            Album.class,
                            R.layout.album_cards,
                            AlbumsViewHolder.class,
                            mAlbumQuery) {
                        @Override
                        public void populateViewHolder(AlbumsViewHolder holder, Album album, int position) {
                            holder.bindView(album.getImage(), album.getTitle());
                            if (!album.getGenres().contains(mRecommendation.getGenre())) {
                                //added as a hypothetical... should i have something in here?
                            }
                        }
                    };
                    mAlbumsRecycler.setAdapter(mAdapter);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return view;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
darkknightsds
  • 530
  • 8
  • 16

2 Answers2

1

if you want to extract any particular node u can use this:-

String notific = String.valueOf(dataSnapshot.getValue());

int key=dataSnapshot.getKey();
String title=String.valueOf(dataSnapshot.child("title").getValue());
String content=String.valueOf(dataSnapshot.child("content").getValue());
neo73
  • 434
  • 5
  • 20
1

Well, if you send mAlbumQuery as param to your FirebaseRecyclerAdapter, I believe, it takes its size as number of items.

As an option (for quick fix) you can create new collection and inside this loop:

for (DataSnapshot reco : dataSnapshot.getChildren()) {
}

you can fill that new collection with needed items.
After loop you can create new adapter and pass filtered collection to it.

Here is how I see this:

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Collection<> myNewCollection = new Collection<>(); //HashMap, ArrayList - depends on what you are storing in Firebase
            for (DataSnapshot reco : dataSnapshot.getChildren()) {
                if (reco.getValue().toString().contains(mRecommendation.getGenre())) {
                    //below returns the items i want
                    Log.d("is this correct", reco.getValue().toString());
                    //below returns everything in the original query
                    //how to populate only items that match the above?
                    myNewCollection.add(reco.getValue);
                }
            }

            recyclerView.setAdapter(new MyRecyclerViewAdapter(myNewCollection, ...));
        }

Also pls take a look at Firebase docs and this SO question.
There are interesting methods - startAt, endAt and equalTo, which might help you. I didn't find method contains, unfortunately, but methods above might be enough for you.

Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • Yeah I think I realize mAlbumQuery needs to change... I am just trying to figure out what should take its place I guess, or maybe I approach this a different way. I'm not sure. Where should I create the new collection you mention? – darkknightsds Jul 26 '17 at 07:32
  • Thank you, I will be working on this today and let you know how it goes. Contains doesn't exist on objects, which is why I had to do toString first. I am searching for a specific keyword within a string property and trying to return only those that have the keyword. – darkknightsds Jul 26 '17 at 18:53