2017-08-25 29 views
0

配列を含むfirebaseデータベースにオブジェクトを保存したい。私はこれを使用する場合データベースに配列を持つオブジェクトを追加するFireBase

は:

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

は、私は私のデータベース内の配列を参照してくださいいけません。

が#####これは私のクラスのユーザ情報である

#####に編集:

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

} 

とそのまだ動作していない、私はMTデータベースのルートに私のハッシュを参照してくださいいけません: 私が使用する場合

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がアレイの使用を推奨している多くの理由の1つは、セキュリティルールを記述することが不可能になるということです。 Firebaseデータベースはキーと値のペアとして構成されているため、最良の選択肢はMapです。

以下は、2つの異なる参照に値を設定する例です。

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上に保存されている場合は、読み書きするfirebaseデータベースルールを変更したことを確認してくださいチェックアウト動作しない場合

ありがとう

関連する問題