
Question:
I want to implement followers functionality in my android application. 'First' image refers to the list of user i followed. 'second' image have all ids of all the posts that users post and 'third' image havin post details. all i want to do is to load only the post of those users that i've followed.
What i am doing is, i get emails from followers node and by using indexed recyclerview i load the data but its not working because recyclerview is not getting dynamic reference. Any sugested solution ?
first
<a href="https://i.stack.imgur.com/cHTX7.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/cHTX7.png" data-original="https://i.stack.imgur.com/cHTX7.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
second
<a href="https://i.stack.imgur.com/QVSJA.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/QVSJA.png" data-original="https://i.stack.imgur.com/QVSJA.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
third
<a href="https://i.stack.imgur.com/B9O97.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/B9O97.png" data-original="https://i.stack.imgur.com/B9O97.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
Answer1:In order to achieve this, you need to query your database for three times like this:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String userEmail = firebaseUser.getEmail();
String encodedUserEmail = userEmail.replace(".", ",");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userEmailFromFollowersRef = rootRef.child("followers").child(encodedUserEmail);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String followerEmail = ds.getKey();
DatabaseReference my_postsRef = rootRef.child("my_posts").child(followerEmail);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String postId = dSnapshot.getKey();
DatabaseReference postsRef = rootRef.child("posts").child(postId);
ValueEventListener vel = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()) {
String postText = child.child("postText").getValue(String.class);
Log.d("TAG", postText);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(vel);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
my_postsRef.addListenerForSingleValueEvent(valueEventListener);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userEmailFromFollowersRef.addListenerForSingleValueEvent(eventListener);