gpt4 book ai didi

java - Firebase数据库如何从1个引用中获取一些值并等于2个引用来获取其键和值

转载 作者:行者123 更新时间:2023-12-01 08:47:29 25 4
gpt4 key购买 nike

我有如下所示的用户信息

user

我想检查 commentWriterUid 是否等于用户 key 中的 1 以获取其信息

comment

我尝试过这个:

Query query = mDatabaseReference_comments.orderByChild("commentWriterUid").equalTo(FirebaseUtil.getUsersReference().getKey());

query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userModel = dataSnapshot.getValue(UserModel.class);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

mFirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CommentModel, CommentViewHolder>(CommentModel.class,
R.layout.layout_comment,
CommentViewHolder.class,
mDatabaseReference_comments) {

@Override
protected void populateViewHolder(CommentViewHolder viewHolder, CommentModel model, int position) {

Glide.with(PostCommentActivity.this)
.load(userModel.getUserImageUrl())
.into(viewHolder.mCircleImageView_commentWriterImage);

viewHolder.mTextView_commentWriterName.setText(userModel.getUserName());

viewHolder.bindToPost(PostCommentActivity.this, model);

}

};

但是我收到这些错误:

03-04 02:24:34.558 31063-31063/com.app E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.app.PostCommentActivity$4.populateViewHolder(PostCommentActivity.java:119)
at com.app.PostCommentActivity$4.populateViewHolder(PostCommentActivity.java:113)

最佳答案

没有人回答我的问题,所以我自己想出了这个问题并回答了这个问题,这样可能对其他人有帮助

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();

final DatabaseReference root_ref = firebaseDatabase.getReference();

root_ref.child("comments").child("KeKwGa5btKuDYr3yYq5").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

String commentWriterUid = dataSnapshot.child("commentWriterUid").getValue().toString();

root_ref.child("users").equalTo(commentWriterUid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

// Do all stuff you went

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

关于java - Firebase数据库如何从1个引用中获取一些值并等于2个引用来获取其键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42588718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com