简体   繁体   中英

How do I call data from Firebase nested in multiple child nodes?

I am having trouble trying to access a nested child node in my firebase real-time database.

The error I am encountering is

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type sg.edu.singaporetech.teamproject.users

This is how my database structure looks like:数据库结构

I have seen solutions from here: How do I access the child nodes nested inside child node in Firebase database?

This is my code in trying to call the data from 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);
            }
        }
    }

This is from my users class

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

So how do I not get the error and obtain specifically the "true" or "false" value as seen in the database structure of the current month, which is march19?

Really appreciate your help.

To retrieve true and false , try the following:

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) {

  }
});

Here you dataSnapshot is at child vouchers , then you easily access april19 and march19 values without iterating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM