Firebase在Android中加入数据 [英] Firebase join data in Android

查看:95
本文介绍了Firebase在Android中加入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Firebase加入三个数据。我想检索学校名称查询到只知道保护键的数据 G1 。我知道它可以在两个不同的调用完成,但我只是在一个调用服务器的时候这样做。



是否有可能?

学校

 学校:
schoolKey1:
名称:School One
schoolKey2:
名称:School Two

课程

<$ p $ 类别:
someKey1:
名称:Class One
school:schoolKey1
guard:g1

警卫

 警卫:
g1:
name:Pipoy Pat


解决方案

可能已经太晚了,但是您需要按照文档这里



类似于:

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

获取类
ref.child(classes)。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot snapshot,String previousChildKey){
//获得学校关键字
字符串schoolKey = snapshot.getKey();

//从关键字$ b $获得学校名称ref.child(schools /+ schoolKey +/name\").addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
System.out.println(是+ snapshot.getValue());
}

@Override
public void onCancelled(FirebaseError firebaseError){
//现在忽略
}
});
}
});


I'm try to join three data using Firebase. I want to retrieve the the school name querying to classes data knowing only guard key which is g1. I know it can be done in two separate calls, but I'm trying to do it in just one call to the server.

Is it somewhat possible?

Schools

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

Classes

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

Guard

guards : 
  g1 :
    name: Pipoy Pat

解决方案

It might be too late but you would need to follow the documentation here.

something like:

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 
          }
        });
      }
    });

这篇关于Firebase在Android中加入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆