I'm still trying to learn about this NoSQL database and its integration with Android.
I had a SQL database before and it has some relations between tables, like this
User
int id
text name
int groupId
Group
int id
text name
In Firebase I implement them like this,
{
"user": {
"1": {
"name": "Jon",
"groups": {
"group1": true
}
}
},
"groups": {
"group1": {
"name": "Group 1"
}
}
}
Till here I think its fine, but when I write some code in Java I don't know how to put my POJO. Here is what I've done till now:
public class Group {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class User {
private String name;
private Group groups;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group getGroups() {
return groups;
}
public void setGroups(Group groups) {
this.groups = groups;
}
}
And now reading the values. (Is on method OnCreate)
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("user");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> children = dataSnapshot.getChildren();
DataSnapshot next = children.iterator().next();
User value = next.getValue(User.class);
Group group = value.getGroups();
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
But my group's value is always null. What am I doing wrong?? How do I relate user with groups?