2017-08-25 72 views
0

我想在我的firebase数据库中保存一个对象,其中包含一个数组。添加一个数组到数据库的对象FireBase

当我使用这个:

mDatabaseReference.child("user").child(id).setValue(user); 

我没有看到我的数据库中的数组。

#####编辑#####

这是我的课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); 

} 

其仍然没有工作,我没有看到我的散列吨数据库的根: 当我使用

UserInformation user=new UserInformation(email1,password1); 
       String id=mDatabaseReference.push().getKey(); 
       mDatabaseReference.child("user").child(id).setValue(user); 
       startActivity(new Intent(getApplicationContext(),ProfileActivity.class)); 

我能做些什么呢?

+0

发布您的firebase树结构,屏幕截图会很好 –

+0

您看到了什么**吗?是否添加了对象的其余部分? –

+0

@ cricket_007是 –

回答

1

Firebase官方文档建议不要使用arrays。试图使用数组,对于Firebase来说是一种反模式。除此之外,Firebase建议不要使用阵列的原因之一是它使安全规则无法写入。由于Firebase数据库的结构是成对的键和值,因此最好的选择是使用Map

下面是在两个不同引用上设置值的示例。

Map<String, Object> childUpdates = new HashMap<>(); 
childUpdates.put("/posts/" + key, postValues); 
childUpdates.put("/user-posts/" + userId + "/" + key, postValues); 
mDatabaseReference.setValue(childUpdates); 

或使用对象:

UserInformation ui = new UserInformation(email, Password); 
mDatabaseReference.setValue(ui); 

希望它能帮助。

+0

感谢它的作品! –

0

将您的对象保存到数据库中。

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); 

} 

现在创建UserInformation类的对象。

 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)); 

如果这个不奏效然后添加事件侦听器和检查数据正在被保存在您的FirebaseDatabase确保你已经改变了火力数据库规则读写

谢谢