[英]How to retrieve values in the firebase realtime database
例如,我想获取第一条记录并获取用户名和密码。 如何获得第一条记录的“密钥”?
这是我的代码:
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.