Here is my database:
I want to get data from stats.20201104.nutrients.protein which should be 2.
For all other nodes above it, I have been able to fetch data pretty nicely with a User POJO class setup. I can't seem to figure out a way to get the date vals, eg, 20201104, generically, it says the following in the run log which is expected:
No setter/field for 20201104 found on class com.example.nutrobud.ui.home.Stats
I know my Stats.java class is not setup correctly, how can I set this up so that I still maintain the same hierarchy of db nodes?
Here is my User class from User.java:
public class User {
private String email;
private String password;
private String firstName;
private String secondName;
private int age;
private String gender;
private int weight;
private List<String> ingredientsNo;
private List<String> ingredientsYes;
private List<Integer> ingredientsYesGoalsQty;
private List<Integer> ingredientsYesTrackedQty;
private int calorieGoalsQty;
private int calorieTrackedQty;
private Stats stats;
//constructors
//basic getter and setters for all data members
}
Stats.java, same directory as User.java:
public class Stats {
private int caloriesTracked;
private HashMap<String, Integer> nutrients;
//constructors
//basic getter and setters for all data members
}
Function to read data from db in activity:
db.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
//do something with user
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
You're close, but right now you have nothing in your POJOs capturing the 20201104
that the error message mentions. Since you can't know the actual keys, those will need to be a map.
So I think your:
private Stats stats;
Should actually be:
private Map<String, Stats> stats;
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.