我想获取所有“user”的值,但调用datasnapshot.getValue返回null。
FirebaseDatabase.getInstance().getReference("data").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
if (dataSnapshot.getKey().equals("user"))
username = data.getValue().toString();
Toast.makeText(getApplicationContext(), "sender is " + username, Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
使用数据库结构创建一个类。
public class Data {
private String msgbody;
private String mtime;
private String uname;
private String user;
public Data() {
}
public String getMsgbody() {
return msgbody;
}
public void setMsgbody(String msgbody) {
this.msgbody = msgbody;
}
public String getMtime() {
return mtime;
}
public void setMtime(String mtime) {
this.mtime = mtime;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
然后
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Data obj = snapshot.getValue(snapshot.getValue(Data.class);
}