【问题标题】:How do I call data from Firebase nested in multiple child nodes?如何调用嵌套在多个子节点中的 Firebase 中的数据?
【发布时间】:2019-03-31 09:56:53
【问题描述】:

我在尝试访问我的 firebase 实时数据库中的嵌套子节点时遇到问题。

我遇到的错误是

com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 sg.edu.singaporetech.teamproject.users 类型

这是我的数据库结构的样子:

我从这里看到了解决方案:How do I access the child nodes nested inside child node in Firebase database?

这是我尝试从 firebase 调用数据的代码:

    DatabaseReference databaseUsers;
    databaseUsers = FirebaseDatabase.getInstance().getReference("users");



        databaseUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //method is called whenever data at location is updated
                showData(dataSnapshot);
                }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        return view;
    }

    private void showData(DataSnapshot dataSnapshot) {
        FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
        String userID = currentUser.getUid();

        for (DataSnapshot uniqueIDSnapshot : dataSnapshot.getChildren()) {
            //Loop 1 to go through all child nodes of users
            for(DataSnapshot vouchersSnapshot : uniqueIDSnapshot.child(userID).getChildren()){
                //loop 2 to go through all the child nodes of vouchers node
                users user = vouchersSnapshot.getValue(users.class);
            }
        }
    }

这是来自我的users 班级

package sg.edu.singaporetech.teamproject;

public class users {

    String id;
    String email;
    String level;
    String steps;
    String vouchers;

    public users() {
    }

    public users(String id, String email, String level) {
        this.id = id;
        this.email = email;
        this.level = level;
        this.steps = steps;
        this.vouchers = vouchers;
    }

    public String getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public String getLevel() {
        return level;
    }

    public String getSteps() { return steps;}

    public String getVouchers() { return vouchers;}

    public void setId(String id) {
        this.id = id;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public void setSteps(String steps) {
        this.steps = steps;
    }

    public void setVouchers(String vouchers) {
        this.vouchers = vouchers;
    }
}

那么如何才能不报错,具体获取当月数据库结构中看到的“真”或“假”值,也就是march19呢?

非常感谢您的帮助。

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    要检索 true 和 false,请尝试以下操作:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userID).child("vouchers");
    mDatabaseUsers.addValueEventListener(new ValueEventListener() {
      @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    
       String values     = dataSnapshot.child("april19").getValue(String.class);
       String boolValues = dataSnapshot.child("march19").getValue(String.class);
    }
    
      @Override
    public void onCancelled(DatabaseError databaseError) {
    
      }
    });
    

    这里 dataSnapshot 位于子 vouchers,然后您无需迭代即可轻松访问 april19 和 march19 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2017-07-13
      • 2019-07-23
      • 1970-01-01
      相关资源
      最近更新 更多