2017-06-15 59 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()方法。

希望它有幫助。