My objective is to get post from user1, user3, user4 which user2 is following.
//Get datasnapshot at your "users" root node
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child
("Users").child("following").child(uid);
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Get map of users in datasnapshot
collectDispayNames((Map<String, Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//handle databaseError
}
});
private void collectDispayNames(Map<String, Object> users) {
displayNames = new ArrayList<>();
//iterate through each following, ignoring their UID
for (Map.Entry<String, Object> entry : users.entrySet()) {
//Get user map
Map singleUser = (Map) entry.getValue();
//Get following_id field and append to list
displayNames.add((String) singleUser.get("who_i_follow"));
//Toast.makeText(this, displayNames.toString(), Toast.LENGTH_LONG).show();
int size = displayNames.size();
if (size > 0){
mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
.child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
}else {
mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
.child("Contents");
}
if (size >1){
mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
.child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
}else {
mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
.child("Contents");
}
}
}
Now let assume i retrieve all users which user2 is following into array called showOwnerId
I can get array of who user2 following, but since it has many people following, how is it possible to insert all the id of users it is following into database query like this
mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
.child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));