如何从firebase数据库中获取我关注的用户的帖子

时间:2017-06-23 04:54:00

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

[我的数据库结构如下] [1]

{

Posts" : {
    "-KnDkoPgr0ePAUS1acRF" : {
      "author" : "fhfgjl",
      "full_url" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/PostPic%2Ftmp5881607398655738702.jpeg?alt=media&token=2f180ded-339d-49dc-9236-76f0850ab2fe",
      "profile" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0022.jpg?alt=media&token=1be56929-a657-4c8c-81f6-1d73d5534b57",
      "starCount" : 1,
      "stars" : {
        "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : true
      },
      "text" : "cheating",
      "timestamp" : 1498117523692,
      "uid" : "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2"
    },
    "-KnDl3vJF8wABvDP9SoE" : {
      "author" : "din",
      "full_url" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/PostPic%2FIMG-20170206-WA0018.jpg?alt=media&token=533406ac-5ca1-4fcd-b0a1-a73cbaa9194e",
      "profile" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0023.jpg?alt=media&token=0cdb8a03-ff4c-4f7f-8cbe-d22d7604b1cd",
      "starCount" : 1,
      "stars" : {
        "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : true
      },
      "text" : "chilling",
      "timestamp" : 1498117591383,
      "uid" : "KOag2Br7MOaJqJNSFbcTD3ljEnh2"
    }
  },
  "Users" : {
    "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : {
      "email" : "shekhar9065@gmail.com",
      "following" : {
        "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : true
      },
      "name" : "raval",
      "posts" : {
        "-KnDkoPdke4p4Ns8NUkR" : true
      },
      "profilepic" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0022.jpg?alt=media&token=1be56929-a657-4c8c-81f6-1d73d5534b57",
      "userName" : "fhfgjl"
    },
    "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : {
      "email" : "raval@gmail.com",
      "following" : {
        "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : true,
        "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : true
      },
      "name" : "nirali",
      "posts" : {
        "-KnDl3vFUlSxyRm0z547" : true
      },
      "profilepic" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0023.jpg?alt=media&token=0cdb8a03-ff4c-4f7f-8cbe-d22d7604b1cd",
      "userName" : "din"
    }
  },
  "UsersPost" : {
    "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : {
      "-KnDkoPdke4p4Ns8NUkR" : {
        "author" : "fhfgjl",
        "full_url" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/PostPic%2Ftmp5881607398655738702.jpeg?alt=media&token=2f180ded-339d-49dc-9236-76f0850ab2fe",
        "profile" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0022.jpg?alt=media&token=1be56929-a657-4c8c-81f6-1d73d5534b57",
        "starCount" : 0,
        "text" : "cheating",
        "timestamp" : 1498117524012,
        "uid" : "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2"
      }
    },
    "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : {
      "-KnDl3vFUlSxyRm0z547" : {
        "author" : "din",
        "full_url" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/PostPic%2FIMG-20170206-WA0018.jpg?alt=media&token=533406ac-5ca1-4fcd-b0a1-a73cbaa9194e",
        "profile" : "https://firebasestorage.googleapis.com/v0/b/picchat-d70e2.appspot.com/o/ProfilePic%2FIMG-20170206-WA0023.jpg?alt=media&token=0cdb8a03-ff4c-4f7f-8cbe-d22d7604b1cd",
        "starCount" : 0,
        "text" : "chilling",
        "timestamp" : 1498117591912,
        "uid" : "KOag2Br7MOaJqJNSFbcTD3ljEnh2"
      }
    }
  },
  "followers" : {
    "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : {
      "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : true
    },
    "KOag2Br7MOaJqJNSFbcTD3ljEnh2" : {
      "8WZ6jPIvVuNUEjR1G1ewEPpr2Ln2" : true
    }
  }

}

我的活动看起来像这样

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_detail);

    currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
    mFollowing = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUser).child("following");
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    posts = new ArrayList<>();
    mRecyclerView = (RecyclerView)findViewById(R.id.following_post_list);
    mRecyclerView.setLayoutManager(mlayoutManager);
    mAdapter = new PostQueryAdapter(posts);
    mRecyclerView.setAdapter(mAdapter);
}

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

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

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


                DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("UsersPost").child(following.getKey());

                mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
                        Post.class , R.layout.post_item , PostViewHolder.class , reference) {
                    @Override
                    protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {

                        viewHolder.bindToPost(model);
                    }
                };
                mRecyclerGrid.setAdapter(mAdapter);

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    }

Recycler适配器如下

public class PostQueryAdapter扩展了RecyclerView.Adapter {

public ArrayList<Post> mdata;

public PostQueryAdapter(ArrayList<Post> data) {
    mdata = data;
}

@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_item, parent, false);
    return new PostViewHolder(view);
}

@Override
public void onBindViewHolder(PostViewHolder holder, int position) {

    Post post = mdata.get(position);
    holder.bindToPost(post);

}

@Override
public int getItemCount() {
    return mdata.size();
}

public void add(int position, Post post) {
    mdata.add(position, post);
    notifyItemInserted(position);
}
I am not getting any thing in recyclerview and also no error in logcat
it is just not displaying any thing.
What is to be change in code ...

1 个答案:

答案 0 :(得分:0)

onDAtaChange内,试试这种方式让我知道

    Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
    while (dataSnapshots.hasNext()) {
        DataSnapshot dataSnapshotChild = dataSnapshots.next();
        String id = dataSnapshotChild.getKey();
        mDatabase.child(id).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // check here the datasnapshot
                Post post = dataSnapshot.getValue(Post.class);
                mAdapter = new PostQueryAdapter(posts);
                mAdapter.add(mAdapter.mdata.size(), dataSnapshot.getValue(Post.class));
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }