如何从嵌套在多个子节点中的Firebase调用数据?

时间:2019-03-31 09:56:53

标签: android firebase firebase-realtime-database

尝试访问Firebase实时数据库中的嵌套子节点时遇到麻烦。

我遇到的错误是

  

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

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

我从这里看到了解决方案: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;
    }
}

那么,如何不获取错误并具体获取在3月19日当月的数据库结构中看到的“ true”或“ false”值?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

要检索else if (message.content.search("/gif") == 0) { let msg = message.content; let gif1 = msg.split("/gif")[1].trim() let gif2 = gif1.replace(" ", "-"); let gif = "https://giphy.com/search/" + gif2; message.channel.send("Most relevant result for" + " " + gif1, {files: [gif]} ) } true,请尝试以下操作:

false

此处dataSnapshot位于子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) { } }); 处,那么您无需迭代即可轻松访问vouchersapril19值。