java - 如何链接使用 push() 方法保存的 firebase 中的两个子数据库?

标签 java android firebase firebase-realtime-database

如何获取firebase实时数据库中push()方法添加的指定数据child?

您好,我在 firebase 实时数据库中有以下数据子项:- enter image description here

我正在使用 child.push() 方法来保存数据,以便在注册 Posts 子项时生成一个随 secret 钥。每个帖子也有一个用户的用户 ID,发布者,这也是一个主键。

以下是我编写新帖子子代码的代码:-

   private void writeNewPost(String userId, String tittle, String address, String locationLat, String locationLong, String date, String extrass, String postId) {
    Alerts alerts = new Alerts(userId, tittle, address, date, locationLat, locationLong, extrass, postId);
    String key =  mDatabaseUsers.push().getKey();

   // String key = mDatabase.child("posts").push().getKey();

    mDatabaseUsers.child("Posts").child(key).setValue(alerts, new DatabaseReference.CompletionListener() {
        public void onComplete(DatabaseError error, DatabaseReference ref) {
            Log.d("ssd", "onComplete: " + error);
        }
    });

}

现在我想检索一个特定的帖子,但我不明白如何,例如我有一个 RecyclerView,我在其中显示所有帖子。当用户点击特定帖子时,该帖子应该与来自 firebase 的数据一起检索,但我不明白如何。我像这样使用 addValueEventListener :-

   mDatabase.child("Posts").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final String key =  mDatabase.push().getKey();
//                    String name = (String) 

dataSnapshot.child("name").getValue();
//                    String mobile = (String) dataSnapshot.child("phoneNumber").getValue();
//                    String additionalIn = (String) dataSnapshot.child("extras").getValue();
//                    String address = (String) dataSnapshot.child("address").getValue();
//                    String profile_pic = (String) dataSnapshot.child("address").getValue();
              String post_uid = (String) dataSnapshot.child("userid").getValue();
              String post_tittle = (String) dataSnapshot.child("tittle").getValue();





//                if (mAuth.getCurrentUser().getUid().equals(post_uid)){

//

    //                    deleteBtn.setVisibility(View.VISIBLE);

//                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d("OMG", "onCancelled: " + databaseError);
        }
    });

    }
}

但我不明白如何在 onClick() 或任何其他类似的回调方法中检索特定的 child 。

最佳答案

根据用户id获取用户信息:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
postRef= FirebaseDatabase.getInstance().getReference().child("Posts").child("Posts");
postRef.orderByChild("tittle").equalTo(post_here).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds: dataSnapshot.getChildren()){
                String userid=ds.child("userid").getValue().toString();
               ref.child("Users").child("Users").orderByKey().equalTo(userid).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot datas: dataSnapshot.getChildren()){
                  String fullname=dataSnapshot.child("address").getValue().toString();
                //other data
                 }
              }
                  @Override
                 public void onCancelled(DatabaseError databaseError) {

                      }
                   });    
              }
          }
      }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
  });
}

用户点击帖子后,您可以使用用户的 ID 检索该帖子的数据,然后您可以通过执行 orderByKey().equalTo(userid)

关于java - 如何链接使用 push() 方法保存的 firebase 中的两个子数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829816/

相关文章:

java - 我收到此错误消息(该进程无法访问该文件,因为它正在被另一个进程使用)

java - Vungle 广告不可用

android - 在 Volley 中重新登录并重试请求

java - 在 ListView 中从 Firebase 数据库获取子数据

java - 更新java数据库中的记录值

java - 如何使用 postman 将用户登录详细信息传递给 Spring Boot Rest API

java - 在 Micronaut 休息端点的 JSON 中包含 null 和空属性

Android 深度链接 - 返回堆栈

android - NoClassDefFoundError FirebaseOptions,安卓?

Firebase - 在建立连接之前关闭 WebSocket。