YPLABS :
how to create realtime firebase subnodes
note I did not put the user changed because I have been trying to add from the mainactivity code and also because everyone I built to make this addition when creating the user went wrong. Thank you very much in advance. any help is welcome.
Hello i'm trying to create subnodes within user has
{
"Users": {
"Hr1riTVGQWWsKnOsT8n7wIBO7R63": {
"address": "asdasd",
"email": "[email protected]",
"fullname": "asdasd",
"helpsms": "123456",
"phone": "55555555555"
}
}
}
wanted to create a node inside users called casino and inside the casino would have the subnodes of the casino inside user
time: with default value of 200 seconds
reward: with default value of 200
but when i try to modify my builder everything goes wrong when i click the button. can anybody help me
Button code:
addButton.setOnClickListener(new View.OnClickListener() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
@Override
public void onClick(View v) {
FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ref.child(userUid).child("cassino").setValue(dataSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
my user.class
public class User {
public String helpsms;
public String email;
public String phone;
public String address;
public User(String helpsms, String email, String phone, String address, String fullname) {
this.helpsms = helpsms;
this.email = email;
this.phone = phone;
this.address = address;
this.fullname = fullname;
}
public String fullname;
}
Peter Haddad :
Change this:
ref.child(userUid).child("cassino").setValue(dataSnapshot);
into this:
Map<String,Object> map = new HashMap<>();
map.put("address",dataSnapshot.child("address").getValue(String.class));
ref.child("Users").child(userUid).child("cassino").setValue(map);
Add the child("Users")
and then add the values