【发布时间】:2017-08-23 00:07:46
【问题描述】:
我正在制作一个社交媒体应用程序,我想为在 firebase 上发布的每个帖子添加一个评论部分。现在我在制作评论部分时遇到了困难。我已经尝试了下面的代码,但它一直在单行上发布 cmets,然后使 firebase 崩溃。它不会为新评论创建单独的行。添加第一条评论后,firebase 数据库如下所示:
但在我添加另一条评论后,“cmets”值会覆盖,而不是创建新的子帖子键。
这就是我将 cmets 发送到 firebase 的方式:
final String message=myComment.getText().toString();
if (!TextUtils.isEmpty(message))
{
DatabaseComment.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("comments").setValue(message);
hDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("othersName")
.setValue(dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("name").getValue());
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("othersDP")
.setValue(dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("DP").getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是我正在制作 recyclerAdapter 的 onStart 方法:
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<Profile,ProfileViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Profile, ProfileViewHolder>(
Profile.class,
R.layout.commentttt_row,
ProfileViewHolder.class,
DatabaseCommenttttt
) {
@Override
protected void populateViewHolder(final ProfileViewHolder viewHolder, final Profile model, int position) {
//viewHolder.setName(model.getName());
viewHolder.setComments(model.getComments());
viewHolder.setOthersName(model.getOthersName());
viewHolder.setOthersDP(model.getOthersDP());
}
};
mProfileList.setLayoutManager(mLayoutManager);
mProfileList.setAdapter(firebaseRecyclerAdapter);
这是我的 RecyclerView 类:
public static class ProfileViewHolder extends RecyclerView.ViewHolder
{
View mView;
LinearLayout mLinearName;
TextView userName;
TextView mComment;
CircleImageView mSetupImageButton;
FirebaseAuth mAuth;
TextView post_name;
private String comments;
private String othersName;
private String othersDP;
public ProfileViewHolder(View itemView) {
super(itemView);
mView=itemView;
userName=(TextView) mView.findViewById(R.id.post_name);
mLinearName=(LinearLayout) mView.findViewById(R.id.layout_name);
mComment=(TextView) mView.findViewById(R.id.comment);
}
public void setImage(final Context ctx, final String image)
{
final ImageView post_image=(ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(image).into(post_image);
}
});
}
public void setComments(String comments) {
TextView post_phone=(TextView) mView.findViewById(R.id.others_comments);
post_phone.setText(comments);
}
public void setOthersName(String othersName) {
TextView post_phone=(TextView) mView.findViewById(R.id.others_name);
post_phone.setText(othersName);
}
public void setOthersDP(String othersDP) {
CircleImageView mSetupImageButton = (CircleImageView) mView.findViewById(R.id.accountImageButton);
Picasso.with(mView.getContext()).load(othersDP).into(mSetupImageButton);
}
}
【问题讨论】:
-
这行是什么意思,“它一直在单行上发布 cmets,然后使 firebase 崩溃”。在您的代码中,您 1 位用户只能发表 1 条评论。其他人也可以发表评论.. 但如果您希望 1 个用户多次发表评论,那么您需要更改一些代码。
-
好的,我应该进行哪些更改才能使单个用户评论多次?
-
这是你的问题吗??或者你还有什么其他的??
-
它将解决我的部分问题。
-
其他是什么??
标签: java android firebase firebase-realtime-database android-recyclerview