簡體   English   中英

如何檢索 Firebase 實時數據庫中的值

[英]How to retrieve values in the firebase realtime database

我正在努力從 Firebase 檢索數據。 在此處輸入圖片說明

例如,我想獲取第一條記錄並獲取用戶名和密碼。 如何獲得第一條記錄的“密鑰”? 在此處輸入圖片說明

這是我的代碼:

    private String UserName;
    private String Password;
    private int Identity;
    private DatabaseReference reff = FirebaseDatabase.getInstance().getReference();

    public User() {

    }

    public User(String UserName, String Password, int Identity) {
        this.UserName = UserName;
        this.Password = Password;
        this.Identity = Identity;

    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String UserName){
        this.UserName = UserName;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public int getIdentity() {
        return Identity;
    }

    public void setIdentity(int identity) {
        Identity = identity;
    }

    public void Save(){
        reff.child("Users").push().setValue(this);
    }
}
User newUser = new User(userName, password, identity);
                newUser.Save();

如果你想獲得你正在寫的項目的密鑰,你可以看到它:

public void Save(){
    DatabaseReference newUserRef = reff.child("Users").push();
    newUserRef.setValue(this);
    System.out.println(newUserRef.getKey());
}

如果您想在閱讀用戶時找到密鑰,可以向他們顯示:

DatabaseReference usersRef = reff.child("Users");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            System.out.println(userSnapshot.getKey());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM