【问题标题】:How to send and then receive data from firebase in a viewholder如何在viewholder中发送然后从firebase接收数据
【发布时间】: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


【解决方案1】:

假设您有一个带有 cmets、otherDP、otherName 的 Comment 类。现在添加一个新成员 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) {

        }
    });
}

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

编辑:当我使用这个时

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

【讨论】:

  • 在哪一行你得到错误..它的空指针..数据库没有问题。我没有写一个工作代码..它可能有一些错误..你的工作是找出来..但是过程是一样的..
  • 我已经解决了这个错误,现在唯一的问题是一个用户不能多次评论,否则它会覆盖之前的评论,我尝试在 setValue() 之前使用 push() 但现在我不知道如何从 firebase 获取价值,因为我不知道 id push() 分配了什么
  • 此代码将解决您的问题..当您向数组列表添加一个新对象并将完整的数组列表放入数据库中时..无法替换..使用此代码..您的数据库结构会改变..给我截图..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 2017-04-30
  • 2014-12-16
  • 2014-03-20
  • 1970-01-01
  • 2011-06-09
相关资源
最近更新 更多