0

My app is displaying all the firebase data in firebaseRecycler Adapter. I want to exclude the data of current online user from displaying. e-g Ibad Ullah login to the app then his data must not be displayed in the recyclerview. I used Query orderByChild but it is hiding all users data. How can I achieve this? Thank You. Below is my code.

Dashboard Activity

if(fAuth.getCurrentUser() != null){
        UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }

        dRef.child(UID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                remainingClicks = String.valueOf(snapshot.child("clicksRemain").getValue(Integer.class));
                showGigCount = String.valueOf(snapshot.child("showGigCount").getValue(Integer.class));
                fiverrLink1 = (String) snapshot.child("gig").getValue();
                String username = (String) snapshot.child("name").getValue();
                clicks.setText(remainingClicks);
                userName.setText(username);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    recyclerView = findViewById(R.id.dashboardRCV);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
  LoadData();

}

private void LoadData() {
    options = new FirebaseRecyclerOptions.Builder<ModelClass>()
            .setQuery(dRef, ModelClass.class)
            .build();
    adapter = new FirebaseRecyclerAdapter<ModelClass, MyViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {

            if (dRef.child(UID).child("clicksRemain").equals(0)){
                zeroClicks.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
            
            holder.previewLink.setURL(model.getGig(), new URLEmbeddedView.OnLoadURLListener() {
                @Override
                public void onLoadURLCompleted(URLEmbeddedData data) {

                    holder.previewLink.title(data.getTitle());
                    holder.previewLink.description(data.getDescription());
                    holder.previewLink.host(data.getHost());
                    holder.previewLink.thumbnail(data.getThumbnailURL());
                    holder.previewLink.favor(data.getFavorURL());
                }
            });


            //This will hide the gig if its showGigCount becomes 0
            Query query = dRef.orderByChild("showGigCount").equalTo(0);
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()){
                        holder.previewLink.setVisibility(View.GONE);
                    }else{
                        holder.previewLink.setVisibility(View.VISIBLE);
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            };
            query.addListenerForSingleValueEvent(valueEventListener);

            holder.previewLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //    String profileLink = getRef(position).child(model.getGig()).toString();
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getGig()));
                    startActivity(browserIntent);

                }
            });
    

Snapshot of Database

Gum Naam
  • 155
  • 13

2 Answers2

0

There is no way to exclude a specific node from the Firebase database results, so the common way to do this is by hiding the node for the current user in the UI:

@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {
   if (/* check whether this node is for the current */) { 
      holder.itemView.setVisibility(View.GONE); 
   }
   else {
      holder.itemView.setVisibility(View.VISIBLE); 
   }
   holder.geglink.setText(model.getGig());
}

Also see:

You'll also either need to get the key from each node into your Java class, or you can keep a separate list of the keys.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
-1

I did what the previous answer told to,

holder.itemView.setVisibility(View.GONE);

…but that created an empty space in the list. So what I did next was:

holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

That fixed my problem!

Below is the complete code for the onBindViewHolder function:

if (model.getUid().equals(currentUserID)) {
    holder.itemView.setVisibility(View.GONE);
    holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
else {
    holder.userName.setText(model.getName());
    holder.userStatus.setText(model.getStatus());
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77