当前位置: 代码迷 >> java >> Firebase在Android中加入数据
  详细解决方案

Firebase在Android中加入数据

热度:87   发布时间:2023-08-04 09:15:45.0

我尝试使用Firebase加入三个数据。 我想检索查询到classes数据的school name ,只知道保护键是g1 我知道它可以在两个单独的调用中完成,但我只是在一次调用服务器时尝试这样做。

有可能吗?

学校

schools : 
  schoolKey1 :
    name: School One
  schoolKey2 :
    name: School Two

classes : 
  someKey1 :
    name: Class One
    school : schoolKey1
    guard : g1

守护

guards : 
  g1 :
    name: Pipoy Pat

可能为时已晚,但您需要按照的文档进行操作。

就像是:

var ref = new Firebase("https://myApp.firebaseio.com/");

    // fetch classes
    ref.child("classes").addChildEventListener(new ChildEventListener() {
      @Override
      public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        //get school key 
        String schoolKey = snapshot.getKey();

        //get school name from key 
        ref.child("schools/" + schoolKey + "/name").addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot snapshot) {
            System.out.println("The school name is " + snapshot.getValue());
          }

          @Override
          public void onCancelled(FirebaseError firebaseError) {
            // ignore for now 
          }
        });
      }
    });
  相关解决方案