1

I am trying to retrieve the data from RealTime database to RecyclerView using java in android studio. I want to get all the users who following the current user in the my App. I want to get the child which is after the Following node directly and when current user id equals to (to)

this picture for the tree that shows Following node Tree:

"Following": {         
   "4vwxBbUkdhOIPsMxRB8FuJJ8Ccs1" : {         
      "Fd3MXjbkTefGrLDjGeoPg20wME72" : {          
        "date" : "٢٠-فبراير-٢٠٢١",         
          "from" : "4vwxBbUkdhOIPsMxRB8FuJJ8Ccs1",        
          "to" : "Fd3MXjbkTefGrLDjGeoPg20wME72"         
        },        
        "zz0u4R7ygYajOvm1ybZY44ygCOj2" : {         
          "date" : "٢٠-فبراير-٢٠٢١",          
          "from" : "4vwxBbUkdhOIPsMxRB8FuJJ8Ccs1",        
          "to" : "zz0u4R7ygYajOvm1ybZY44ygCOj2"         
        }            
    }            
  }

I mean.. I want to get this child 4vwxBbUkdhOIPsMxRB8FuJJ8Ccs1 when the current user equals to the value of (to) and this my code for retrieve the data to RecyclerView:

private  void initialize()
{
    firebaseAuth= FirebaseAuth.getInstance();
    currentUserId=firebaseAuth.getCurrentUser().getUid();
    followerReference= FirebaseDatabase.getInstance().getReference().child("Following");
    userReference= FirebaseDatabase.getInstance().getReference().child("Users");
    follower_recycler=root.findViewById(R.id.follower_recycler);
    follower_recycler.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    follower_recycler.setLayoutManager(linearLayoutManager);
}

 private void displayFollowers()
{


  Query myFollowers=followerReference.orderByChild("to").equalTo(currentUserId);
    FirebaseRecyclerOptions<Following> options=
            new FirebaseRecyclerOptions.Builder<Following>()
                    .setQuery(myFollowers,Following.class)
                    .build();
    FirebaseRecyclerAdapter<Following, FollowingViewHolder> firebaseRecyclerAdapter=
            new FirebaseRecyclerAdapter<Following, FollowingViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull FollowingViewHolder holder, int position, @NonNull Following model) {
                  final   String  usersId=getRef(position).getKey();
                    userReference.child(usersId).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            if(snapshot.exists())
                            {
                                final String userName=snapshot.child("userName").getValue().toString();
                                holder.setUserName(userName);
                                if(snapshot.hasChild("ProfileImage"))
                                {
                                    final String profileUser=snapshot.child("ProfileImage").getValue().toString();
                                    holder.setProfileImage(getActivity(),profileUser);
                                }
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
                }

                @NonNull
                @Override
                public FollowingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.users_design,parent,false);
                    FollowingViewHolder viewHolder=new FollowingViewHolder(view);
                    return  viewHolder;
                }
            };
    firebaseRecyclerAdapter.startListening();
    follower_recycler.setAdapter(firebaseRecyclerAdapter);
}

and this is my Following class:

public class Following {
    private String date,from,to;

    public Following() {

    }
    public Following(String date,String from,String to) {
        this.date = date;
        this.from=from;
        this.to=to;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

Finally that is the FollowingViewHolder class:

public class FollowingViewHolder extends RecyclerView.ViewHolder {

    public View mView;
    /*some attributes*/
    public FollowingViewHolder(@NonNull View itemView) {
        super(itemView);
        mView=itemView;
       /*initialization for attributes */
    }

    public void setDate(String date)
    {
       /* set data in TextView ..*/
    }

    public void setUserName(String userName){
        /* set data in TextView ..*/
    }
    public void setProfileImage(Context ctx, String profileImage){

        /* set data in circleImage ..*/
    }
}

I Hope you understood my question and get an answer for me:)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Atheer
  • 61
  • 1
  • 5
  • Firebase queries work on a flat list of nodes. So they take each node under the location you query, order them on the value you indicate, and then filter. This means that the value must exist at a fixed path under each direct child node, which is not the case in your data model. So there's no way to query across all `Following` for values of the `to` property. You'll either need to know the first level UID, and query from that node, or you'll need to create a flat list of nodes. See https://stackoverflow.com/q/40656589 and https://stackoverflow.com/q/27207059 for more info. – Frank van Puffelen Feb 20 '21 at 22:07

0 Answers0