2017-06-05 27 views
1

我一直在嘗試從我存儲在Firebase實時數據庫中的數據中檢索數據字段,並且卡住了。如何檢索Firebase數據庫條目的具體內容?

這是我的數據庫是什麼樣子: firebase realtime database

這是一個我已經叫我AuthStateListener對象,定義如下方法:

private void alterTextView(final String id) { 
    if(id!=null) { 
     mDatabase.child("Users").child(id).addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot ds : dataSnapshot.getChildren()) { 
        if (ds.exists()) { 
         UserInformation userInformation = ds.child(id).getValue(UserInformation.class); 
         String name = userInformation.getName(); 
         mWelcomeUserMessage.setText(name); 
         //logging name 
         Log.d("My activity", "name of user with userID - " + id + " is : " + userInformation.getName()); 
        } 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
} 

這個函數的調用是因此:

mAuthStatelistener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      //if user is logged in, the sign_in and sign_up buttons should not be displayed 
      if (firebaseAuth.getCurrentUser() != null) { 
       userId = firebaseAuth.getCurrentUser().getUid(); 
       Log.d("Main Activity", "Current userId : " + userId); 
       alterTextView(userId); 
       mLogInButton.setVisibility(View.INVISIBLE); 
       mSignUpButton.setVisibility(View.INVISIBLE); 
       //getUserName(firebaseAuth.getCurrentUser().getUid()); 
       mLogOutButton.setVisibility(View.VISIBLE); 
      } else { 
       mLogInButton.setVisibility(View.VISIBLE); 
       mSignUpButton.setVisibility(View.VISIBLE); 
       mWelcomeUserMessage.setVisibility(View.GONE); 
       mLogOutButton.setVisibility(View.GONE); 
      } 
     } 
    }; 

出於某種原因,我不斷收到空指針例外的代碼第一段摘錄行我在哪裏存儲userInformation對象的名稱 -

String name = userInformation.getName(); 

我只是不明白爲什麼會發生這種情況。

我的主要目標是打印出已登錄的用戶,一旦他/她已登錄。

的名字有誰知道一個辦法做到這一點?

更新1這是我UserInformation類的樣子:

public class UserInformation { 

private String name; 
private String student_number; 
private String faculty; 
private boolean isAdmin; 

public String getName() { 
    return name; 
} 

public String getStudent_number() { 
    return student_number; 
} 

public String getFaculty() { 
    return faculty; 
} 

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

public boolean isAdmin() { 
    return isAdmin; 
} 

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

public void setStudent_number(String student_number) { 
    this.student_number = student_number; 
} 

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

回答

3

不需要你onDataChange()方法中的循環:

public void onDataChange(DataSnapshot dataSnapshot) { 
    if (ds.exists()) { 
     UserInformation userInformation = ds.child(id).getValue(UserInformation.class); 
     String name = userInformation.getName(); 
     mWelcomeUserMessage.setText(name); 
     //logging name 
     Log.d("My activity", "name of user with userID - " + id + " is : " + userInformation.getName()); 
    } 
} 

當您使用時,才需要一個循環具有值監聽器的查詢,因爲查詢可以有多個結果。但在你的情況下,你直接訪問正確的子節點,所以不需要循環。

2

您試圖訪問一個不存在的孩子,這就是您得到空值的原因。你的聽衆已經聽 「ID子」,替換下面的行:

UserInformation userInformation = ds.child(id).getValue(UserInformation.class); 

通過:

UserInformation userInformation = ds.getValue(UserInformation.class); 

然後,回到你的UserInformation.class,添加以下變化:

@IgnoreExtraProperties // Add this tag 
public class UserInformation { 

    private String name; 
    private String student_number; 
    private String faculty; 
    private boolean isAdmin; 

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

} 
+0

我做了改變,但現在,而不是空指針異常,當我嘗試調用我的textView setText(),我什麼也沒有得到。 此外,這顯示在我的日誌中: 06-05 16:10:09.022 4812-4812/com.example.android.fireapp D /我的活動:具有用戶ID的用戶的名稱 - ITDpaqPfEFY7AdE20vjHbeU84eV2是:null。 **由於某些原因,該字段仍爲空** –

+0

您可以發佈您的UserInformation.java嗎? @YashChowdhary – AlexTa

+0

所以我在默認構造函數中給了它一個默認值「」,這就是顯示的內容。看起來getValue()方法不會改變'name'屬性 –

相關問題