如何在Firebase实时数据库查询中获得所有朋友和当前用户的帖子?

时间:2019-04-11 18:15:49

标签: java android firebase firebase-realtime-database

UPDATE1:我仍然在处理它,但是我无法解决它,也许是在重组数据库?我会继续思考,但是如果有人有想法,那将很高兴听到

好吧,我试图在Firebase查询中让所有当前用户的朋友和他自己的帖子,但我却无法获取,我总是尝试只得到一个朋友的帖子,因为其他朋友的帖子被覆盖了。让我向您展示我的数据库(JSON)的结构以及如何在Android项目Activity中显示该帖子。

UserRef = FirebaseDatabase.getInstance().getReference().child("Users");
PostRef = FirebaseDatabase.getInstance().getReference().child("Post");

...
...
 UserRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                if(dataSnapshot.exists())
                {

                    if(dataSnapshot.hasChild("Friends")){
                        for(DataSnapshot ds : dataSnapshot.child("Friends").getChildren()){
                            friendsID = ds.getKey(); //String with friendsID
                            DisplayAllUserPosts();
                        }
                    }...
...
  private void DisplayAllUserPosts() {

        Query SortPost = PostRef.orderByChild("uid").equalTo(friendsID);

        FirebaseRecyclerOptions<Posts> options=new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPost,Posts.class).build(); \\Posts class is just for getting data base Post nodes elements
        FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options) {...

我的Firebase实时数据库的结构如下:

{
"Post" : {
    "10-04-201918:32:09" : {
      "counter" : 0,
      "date" : "10-04-2019",
      "fullname" : "Xuban Illarramendi",
      "postimage" : "XXXX",
      "profileimage" : "XXXX",
      "status" : "",
      "time" : "18:32:09",
      "uid" : "FP1YJnlJBpfU5aSZlSAErLODWZl2"
    },
    "10-04-201921:10:06" : {
      "counter" : 0,
      "date" : "10-04-2019",
      "fullname" : "Ander Lopez",
      "postimage" : "XXXX",
      "profileimage" : "XXXX",
      "status" : "",
      "time" : "21:10:06",
      "uid" : "8wXFsw1pBRYXBVtChqY1Kqi14dy1"
    }
  },
  "Users" : {
    "8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
      "Friends" : {
        "FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
          "date" : "10-04-2019"
        },
        "sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
          "date" : "10-04-2019"
        }
      },
      "country" : "Spain",
      "dob" : "none",
      "fullname" : "Ander Lopez",
      "gender" : "none",
      "profileimage" : "XXXX",
      "relationshipstatus" : "none",
      "status" : "",
      "username" : "anderlopz"
    },
    "FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
      "Friends" : {
        "8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
          "date" : "10-04-2019"
        },
        "sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
          "date" : "10-04-2019"
        }
      },
      "country" : "Spain",
      "dob" : "none",
      "fullname" : "Xuban Illarramendi",
      "gender" : "none",
      "profileimage" : "XXXX",
      "relationshipstatus" : "none",
      "status" : "",
      "username" : "xuban"
    },
    "sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
      "Friends" : {
        "8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
          "date" : "10-04-2019"
        },
        "FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
          "date" : "10-04-2019"
        }
      },
      "country" : "Spain",
      "dob" : "none",
      "fullname" : "Unai Lopez",
      "gender" : "none",
      "profileimage" : "XXXX",
      "relationshipstatus" : "none",
      "status" : "",
      "username" : "unailopz"
    }
  }
}

所以我的真正问题是他只显示最后一个朋友的帖子,如何在不覆盖以前的friendsID的情况下进行包含我所有friendsID的查询?

0 个答案:

没有答案