Android: How to use conditional DataSnapshot values in Firebase RecyclerView? -
i trying query firebase , populate recycler adapter conditional data query's datasnapshot. tried putting populate function inside if statement correctly logs data want, recycler view instead returns node searching in (the main query started with). suggestions on how populate items apply "if" statement? thank you!
rootref = firebasedatabase.getinstance().getreference(); //below node query malbumquery = rootref.child(constants.firebase_child_albums).orderbychild("genres"); malbumquery.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot reco : datasnapshot.getchildren()) { if (reco.getvalue().tostring().contains(mrecommendation.getgenre())) { //below returns items want log.d("is correct", reco.getvalue().tostring()); //below returns in original query //how populate items match 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 hypothetical... should have in here? } } }; malbumsrecycler.setadapter(madapter); } } } @override public void oncancelled(databaseerror databaseerror) { } }); return view; }
well, if send malbumquery
param firebaserecycleradapter
, believe, takes size number of items.
as option (for quick fix) can create new collection , inside loop:
for (datasnapshot reco : datasnapshot.getchildren()) { }
you can fill new collection needed items.
after loop can create new adapter , pass filtered collection it.
here how see this:
@override public void ondatachange(datasnapshot datasnapshot) { collection<> mynewcollection = new collection<>(); //hashmap, arraylist - depends on storing in firebase (datasnapshot reco : datasnapshot.getchildren()) { if (reco.getvalue().tostring().contains(mrecommendation.getgenre())) { //below returns items want log.d("is correct", reco.getvalue().tostring()); //below returns in original query //how populate items match above? mynewcollection.add(reco.getvalue); } } recyclerview.setadapter(new myrecyclerviewadapter(mynewcollection, ...)); }
also pls take @ firebase docs , this question.
there interesting methods - startat
, endat
, equalto
, might you. didn't find method contains
, unfortunately, methods above might enough you.
Comments
Post a Comment