Reputation: 1124
I want to save an object in my firebase database, which contains an array.
when i use this:
mDatabaseReference.child("user").child(id).setValue(user);
i dont see the array in my database.
#####edited#####
this is my class UserInformation:
public class UserInformation {
private String email1;
private String Password1;
public HashMap<String, Boolean> levels=new HashMap<>();
public UserInformation (String email, String Password){
this.email1=email;
this.Password1=Password;
levels.put("level1", false);
levels.put("level2", false);
levels.put("level3", false);
}
and its still not working, i dont see my hash in the root of mt data base: when I use
UserInformation user=new UserInformation(email1,password1);
String id=mDatabaseReference.push().getKey();
mDatabaseReference.child("user").child(id).setValue(user);
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
what can i do?
Upvotes: 1
Views: 2404
Reputation: 3112
To save your object into the Database.
public class UserInformation {
private String email1;
private String Password1;
public HashMap<String, Boolean> levels=new HashMap<>();
public UserInformation (String email, String Password){
this.email1=email;
this.Password1=Password;
levels.put("level1", false);
levels.put("level2", false);
levels.put("level3", false);
}
Now create the object of your UserInformation class.
UserInformation user=new UserInformation(email1,password1);
DatabaseReference userReference = mDatabaseReference.child("user");
String id=userReference.push().getKey();
userReference.child(id).setValue(user);
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
If this doesn't work out then add the event listener and check if data is being saved on your FirebaseDatabase
and be sure that you have changed the firebase database rules to read and write
Thanks
Upvotes: 0
Reputation: 138824
Official documentation of Firebase recommends against using arrays
. Trying to use an array, is an anti-pattern when it comes to Firebase. Beside that, one of the many reasons Firebase recommends against using arrays is that it makes the security rules impossible to write. Because a Firebase database is structured as pairs of key and values, the best option is to use a Map
.
Below an example of setting a value on two different references.
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabaseReference.setValue(childUpdates);
or using objects:
UserInformation ui = new UserInformation(email, Password);
mDatabaseReference.setValue(ui);
Hope it helps.
Upvotes: 1