【问题标题】:get children from firebase?从火力基地得到孩子?
【发布时间】:2018-01-14 04:08:24
【问题描述】:

我有以下结构

root
    -sections
        -section1
            -student1
                -GPA
                -lastname
            -student2
                -GPA
                -lastname
        -section2
            -student1
                -GPA
                -lastname

我需要获得用户想要的学生的 GPA

我正在这样做:

String student = student.getText().toString(); //Student1/Student2 entered
String section = section.getText().toString();
    //This is Section1, Student1, say (obtained from EditText)...

那我在做

myref = FirebaseDatabase.getInstance().getReference().child("sections");
myref1 = myref.child(section).child(student).child("GPA");
myref1.addValueEventListener(new ValueEventListener() {
  public void onDataChange(DataSnapshot dataSnapshot) {
    String gpa = dataSnapshot.getValue().toString();
  }
  public void onCancelled(DatabaseError databaseError) {}
});

我得到 0.0 作为结果。

我哪里错了?

【问题讨论】:

  • 这部分学生的GPA可能是0.0。您是否调试过您的应用程序并了解它为什么会打印当前结果?
  • 是的,我已经在 LOG 和 textView 中打印了值(以防万一)。这就是我知道值是 0.0
  • 在应用程序上打印为 0.0 之前,该学生在 firebase 数据库中该部分的 GPA 是多少?
  • 它是一个介于 3 和 4 之间的值(例如:3.1、3.75、4.0 等)
  • 也许您需要在问题中包含更多代码。从我所见,您的代码看起来不错。

标签: java android firebase firebase-realtime-database


【解决方案1】:

通常用户需要进行身份验证才能读取或写入数据库。您是否在控制台中禁用了规则或是否有用户登录?

您可以在这张图片中看到规则选项卡:https://www.novoda.com/blog/content/images/2016/06/Screenshot_20160621_174302.png

编辑

代码似乎没问题,可能键“section”和“student”有问题。您可以使用硬编码字符串对其进行测试。

【讨论】:

  • @janik:这也是我的第一个想法。但是如果用户没有权限,onDataChange就不会被调用。
  • @Frank van Puffelen:代码似乎没问题,也许键“section”和“student”有问题。您可以使用硬编码字符串对其进行测试
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 2016-09-17
  • 2017-10-22
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多