Avinash Prabhakar
Avinash Prabhakar

Reputation: 483

Adding data to children within children in firebase

week data

I am able to update data like address and description by doing the following:

FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
       String uid = current_user.getUid();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);//.getReference points to root.
        HashMap<String,Object> usermap = new HashMap<>();
        usermap.put("name",name);
        usermap.put("description",description);
        usermap.put("address",address);
        mDatabase.updateChildren(usermap);

But how do I add Week? Week has five children which are the days.Within each child, I would like an array of integer or string characters. I tried the same way as above but it does not seem to be working. The above method only works for key:value.

Upvotes: 0

Views: 46

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The week is a nested Map, so:

FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
String uid = current_user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);//.getReference points to root.
HashMap<String,Object> usermap = new HashMap<>();
usermap.put("name",name);
usermap.put("description",description);
usermap.put("address",address);

Map<String,String> week = new HashMap<>();
week.put("Monday", "1,1,1,1,1,1,1,1");
week.put("Tuesday", "1,1,1,1,1,1,1,1");
week.put("Wednesday", "1,1,1,1,1,1,1,1");
week.put("Thursday", "1,1,1,1,1,1,1,1");
week.put("Friday", "1,1,1,1,1,1,1,1");

usermap.put("Week", week);

mDatabase.updateChildren(usermap);

Upvotes: 1

Rainmaker
Rainmaker

Reputation: 11090

You should crate a POJO model "Week" that would correspond to a desired structure of your node , then create an instance of this object class and push in to Firebase

Refer this tutorial

Upvotes: 1

Related Questions