因此,我试图显示与已登录用户有关的数据,但与之相关的数据却使我返回null我尝试了多种解决方案,但没有用
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
DatabaseReference userdata;
String namedata;
public HomeViewModel() {
userdata = FirebaseDatabase.getInstance().getReference();
userdata.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snap : dataSnapshot.getChildren()){
User user = snap.getValue(User.class);
namedata = user.getUserId();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mText = new MutableLiveData<>();
mText.setValue("Welcome back! " +namedata);
}
public LiveData<String> getText() {
return mText;
}}`
这是我想要获取这些数据并将其显示在视图中的User.class
public class User {
String userId;
String userPhone;
String userGender;
public User() {
}
public User(String userId, String userPhone, String userGender) {
this.userId = userId;
this.userPhone = userPhone;
this.userGender = userGender;
}
public String getUserId() {
return userId;
}
public String getUserPhone() {
return userPhone;
}
public String getUserGender() {
return userGender;
}}
答案 0 :(得分:0)
您必须调用“ userId”
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
HashMap<String, String> map = (HashMap<String, String>) snap.getValue();
if (map.containsKey("userId")) {
if (map.get("userId").equals("william henry")) {
String userGender = map.get("userGender");
}
}
}
}
或
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snap : dataSnapshot.getChildren()){
HashMap<String, String> map = (HashMap<String, String>) snap.getValue();
if (map.containsKey("userId")) {
String userGender = map.get("userId");
}
}
}
答案 1 :(得分:0)
第一个FirebaseDatabase.getInstance().getReference()
指向您的根目录(这是数据库上方的链接)。因此,您需要指向您想要获得的内容(即用户树)。
其次,您必须将数据设置在onDataChange内,因为它不具有同步功能(这意味着您的姓名数据始终不遵循从onDataChange返回的数据)
FirebaseDatabase.getInstance().getReference().child("User").orderByChild("userId").equalTo(*your_logged_in_user_id*).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Iterator iterator = dataSnapshot.getChildren().iterator()
if(iterator.hasNext()){
User user = iterator.next().getValue(User.class)
}
name = user.getName();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
答案 2 :(得分:0)
由于没有从正确的节点获取值,因此您将获得null。有用户子级,您想获取用户子级的值。
userdata = FirebaseDatabase.getInstance().getReference().child("User");
userdata.addOnChildEventListener( new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snap, String previousChildName) {
//get value here.
User user = snap.getValue(User.class);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onCancelled(DatabaseError error) {
}
});
您想获取单个用户数据,然后 userdata = FirebaseDatabase.getInstance()。getReference()。child(“ User”)。child(userIdOfthatuser);
userdata.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snap) {
User user = snap.getValue(User.class);
namedata = user.getUserId();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});