2017-06-15 60 views
-1

我认为如果我解释我真正想要的会更好。这是我的数据库看起来像: enter image description here如何动态添加对象到firebase中的子项?

我想为每个创建的比赛添加锦标赛状态,但我面临数据检索问题。我有一个名为UserInformation的自定义类,它与dataSnapshot.getValue()方法一起使用,但我不确定如何动态地将比赛对象(使用2个布尔值)添加到用户。我尝试添加一个所述对象的数组列表,但这并不反映在数据库中。

这是我创建一个用户代码:

mAuth.createUserWithEmailAndPassword(emailId, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (task.isSuccessful()) { 
         String userId = mAuth.getCurrentUser().getUid(); 

         DatabaseReference currentUser = mDatabase.child(userId); 
         currentUser.child("name").setValue(name); 
         currentUser.child("studentNumber").setValue(studentNumber); 
         currentUser.child("faculty").setValue(mFaculty); 
         currentUser.child("email").setValue(emailId); 
         currentUser.child("isGod").setValue(false); 
         currentUser.child("tournamentStatuses").setValue(new ArrayList<TournamentStatus>()); 

         Toast.makeText(RegisterActivity.this,"User successfully registered\nLogged in as "+name,Toast.LENGTH_SHORT).show(); 
         mProgressDialog.dismiss(); 
        } 

我的直觉是,这个问题是与ArrayList的。 另外,即使我可以添加用户,我将如何检索特定的锦标赛状态?

这里是我的UserInformation类:

public class UserInformation { 

    public String name; 
    public String studentNumber; 
    public String faculty; 
    public boolean isAdmin; 
    public String email; 
    public boolean isGod; 
    public ArrayList<TournamentStatus> tournamentStatuses; 

    //Define this constructor 
    public UserInformation() { 
     // Default constructor required for calls to DataSnapshot.getValue(UserInformation.class) 
    } 


    public boolean isGod() { 
     return isGod; 
    } 

    public void setGod(boolean god) { 
     isGod = god; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getStudentNumber() { 
     return studentNumber; 
    } 

    public void setStudentNumber(String studentNumber) { 
     this.studentNumber = studentNumber; 
    } 

    public String getFaculty() { 
     return faculty; 
    } 

    public void setFaculty(String faculty) { 
     this.faculty = faculty; 
    } 

    public boolean isAdmin() { 
     return isAdmin; 
    } 

    public void setAdmin(boolean admin) { 
     isAdmin = admin; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public ArrayList<TournamentStatus> getTournamentStatuses() { 
     return tournamentStatuses; 
    } 

    public void setTournamentStatuses(ArrayList<TournamentStatus> tournamentStatuses) { 
     this.tournamentStatuses = tournamentStatuses; 
    } 
} 

有没有办法解决,或者我应该改变我的数据库设计?

+0

Humm,我不明白你的db设计和java中的对象是一样的。您的user-> tournamentStatus不包含多个锦标赛信息。因此它不会填充userInfo中的ArrayList。请添加锦标赛类。 –

回答

1

为了让您的工作更轻松,您需要更改一点数据库设计。在tournamentStatuses代替直接使用tournamentId像这样的节点的名称:

Firebase-root 
    | 
    ---- Tournaments 
    |  | 
    |  ---- // 
    ---- Users 
      | 
      ---- userId 
        | 
        ---- // user details 
        | 
        ---- tournamentId 
          | 
          ---- isOrganizing: false 
          | 
          ---- isPaticipating: false 

通过这种方式,你可以查询你的数据库更容易这样的:

DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child(tournamentId); 

其次,为了写数据对象添加到您的Firebase数据库中,而不使用List,请使用Map。您需要在pojo中进行此更改,并且还需要使用setValue()方法。

希望它有帮助。