Firebase仅获得以下用户的帖子

时间:2017-06-24 12:37:20

标签: android firebase firebase-realtime-database firebase-authentication

我想要显示用户ID跟随的帖子。 我正在使用FirebaseRecyclerAdapter获取帖子。

我的数据库ID结构如下所示是json

"Allposts" : {

"-LnDkoPgr0fPAUSnackF" : {
  "author" : "jack",
  "postimage" : "posturl",
  "profile" : "profileurl",
  "uid" : "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2"
},
"-Lnll3vNF8wABv899SoQ" : {
  "author" : "merry",
  "postimage" : "posturl",
  "profile" : "profileurl",
  "uid" : "KOag2Br7MOaJqJNSFbcTD3ljEnh2"
}

"Users" : {
    "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : {
      "following" : {
        "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : true
      },
      "name" : "username",
      "posts" : {
        "-KnDkoPdke4p4Ns8NUkR" : true
      },

      "profilepic" : "profileurl",
      "userName" : "username"
    },
    "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : {

      "following" : {
        "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : true,
        "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : true
      },
      "name" : "name",
      "posts" : {
        "-KnDl3vFUlSxyRm0z547" : true
      },
      "profilepic" : "profileurl",
      "userName" : "username"
    }
  },

"UsersPost" : {
    "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : {
      "-KnDkoPdke4p4Ns8NUkR" : {
        "author" : "jack",
        "full_url" : "posturl",
        "profile" : "profileurl",
        "uid" : "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2"
      }
    },
    "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : {
      "-KnDl3vFUlSxyRm0z547" : {
        "author" : "marry",
        "full_url" : "posturl",
        "profile" : profileurl",
        "uid" : "KOag2Br7MOaJqJNSFbcTD3ljEnh2"
      }
    }
  },

},

我使用以下方法获取以下帖子

mFollowing = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUser).child("following");
mDatabase = FirebaseDatabase.getInstance().getReference().child("UsersPost");

protected void onStart() {
        super.onStart();

        mFollowing.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot following : dataSnapshot.getChildren()){

                   DatabaseReference ref =  mDatabase.child(following.getKey());
                    mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class,
                            R.layout.post_item,PostViewHolder.class,ref) {
                        @Override
                        protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {
                            viewHolder.bindToPost(model);
                        }
                    };
                    mRecyclerView.setAdapter(mAdapter);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

但它只显示了最后一个用户的帖子:

Users {
    following {
        userid (1)
        userid (2)
        userid (3)
    }
}

只有uid 3的帖子显示在uid 1&amp; 1的帖子后面。 2未显示

如何在下一个节点下显示所有用户的帖子?

2 个答案:

答案 0 :(得分:0)

您需要从mRecyclerView.setAdapter(mAdapter);获取for loop,并在for循环结束后放置它。

希望它有所帮助。

答案 1 :(得分:0)

您本身不需要datasnapshot。这应该可以正常使用,因为您正在使用FirebaseRecyclerAdapter

mFollowing = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUser).child("following");
mDatabase = FirebaseDatabase.getInstance().getReference().child("UsersPost");

protected void onStart() {
        super.onStart();    

DatabaseReference ref =  mDatabase.child(following.getKey());
                    mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class,
                            R.layout.post_item,PostViewHolder.class,ref) {
                        @Override
                        protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {
                            viewHolder.bindToPost(model);
                        }
                    };
                    mRecyclerView.setAdapter(mAdapter);
                }

  }

}