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

时间:2017-03-03 21:27:05

标签: java android firebase firebase-realtime-database

我的用户信息如下所示

user

我想检查commentWriterUid是否等于来自用户密钥以获取其信息

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)

1 个答案:

答案 0 :(得分:1)

没有人回答我的问题,所以我认为这是我自己的问题并回答这个问题,这对其他人来说可能会有所帮助

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) {

            }
        });