如何在视图中发送然后从firebase接收数据

时间:2017-08-23 00:07:47

标签: java android firebase firebase-realtime-database android-recyclerview

我正在制作社交媒体应用,我想为firebase上发布的每篇帖子添加评论部分。现在我在撰写评论部分时遇到了困难。我已经尝试了以下代码,但它会不断在单行上发布评论,然后使firebase崩溃。它没有为新评论创建单独的行。添加第一条评论后,firebase数据库看起来像:enter image description here

但是在我添加另一条评论后,#34;评论"值覆盖而不是创建新的子密钥。

这就是我向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) {

            }
        });
    }

这是我的onStart方法,我正在制作recyclerAdapter:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

假设您有一个带有注释的Comment类,otherDP,otherName。现在添加一个新成员userId。

final String message=myComment.getText().toString();
if (!TextUtils.isEmpty(message))
{
    DatabaseComment.child(mPostKey).addValueEventListener(new 
          ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           ArryList<Comment> comments= (ArryList<Comment>) 
               dataSnapshot.getValue();
            Comment comment = new Comment();
            comment.setName("Name");
            comment.setOtherDP("OtherDP");
            comment.setComment("Comment");
            comment.setUserID("UserID");
            comments.add(comment);
            DatabaseComment.child(mPostKey).setValue(comments);

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

这将在每个帖子中创建一个评论列表..一个用户可以多次评论。

编辑:当我使用此

final String message=myComment.getText().toString();
    if (!TextUtils.isEmpty(message))
    {
        DatabaseComment.child(mPostKey).addValueEventListener(new
              ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
                      ArrayList<Profile> comments= (ArrayList<Profile>)
                              dataSnapshot.getValue();
                      Profile comment = new Profile();
                      comment.setOthersName("Name");
                      comment.setOthersDP("OtherDP");
                      comment.setComments("Comment");
                      comment.setUid("UserID");
                      comments.add(comment);
                      DatabaseComment.child(mPostKey).setValue(comments);

                  }

                  @Override
                  public void onCancelled(DatabaseError databaseError) {

                  }
              });
    }

lOGCAT给我这个:java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.IWindowSession.remove(android.view.IWindow)' on a null object reference