Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude item from a FirebaseRecyclerAdapter

I have this code in my populateViewHolder:

public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
            String k = getRef(position).getKey();
            ref.child(k).child("users").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (!dataSnapshot.hasChild(getUid())) {
                       //Something...
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            //...Populating the viewholder...

I want to remove the item if !dataSnapshot.hasChild(getUid()) is true, otherwise just keep on populating the ViewHolder normally. Note that I don't want to remove the item from the database, I only want it gone from the FirebaseRecyclerAdapter. What method is available for doing this?


Edit: I am STILL looking for a solution for this problem as it has not been fixed. My solution is HIGHLY discouraged and very bad practice, and I have reconsidered using it myself.

like image 914
Ali Bdeir Avatar asked Sep 07 '16 03:09

Ali Bdeir


2 Answers

Best practice would be to use server side filtering: https://firebase.google.com/docs/database/android/retrieve-data#sorting_and_filtering_data

At the moment there is no way to filter your results client side with FirebaseUI (however I do believe it is planned to be implemented in the future):
https://github.com/firebase/FirebaseUI-Android/issues/135

like image 146
Riley MacDonald Avatar answered Nov 15 '22 10:11

Riley MacDonald


Update: This is extremely bad practice and very glitchy. I HIGHLY discourage ANYONE to use this since it will make your RecyclerView very glitchy and will cause excess data loading, which is NOT what your users want.

I ended up making it manually as so:

if(x)
   viewHolder.itemView.setVisibility(View.GONE); //itemView is a view provided by Android, no need to initiate it.

This says, if x is true, get the row and set its visibility to GONE. This solved the problem for me. I will probably fork this on GitHub.

like image 1
Ali Bdeir Avatar answered Nov 15 '22 11:11

Ali Bdeir