2017-08-25 52 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確保你已經改變了火力數據庫規則讀寫

謝謝