當我調試此代碼時,在databaseUser dataSnapshot中有來自用戶在數據庫中的值,但在對象user1
中沒有添加,它全爲空。我不明白,因爲在對象事件中正常接收值。如何從dataSnapshot獲取價值?
databaseEvent.child(getKeyEvent()).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final Event event = dataSnapshot.getValue(Event.class);
user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null) {
databaseUser.orderByChild("userEmail").equalTo(user.getEmail()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//User already exists
User user1 = dataSnapshot.getValue(User.class);
}
}
});
我的數據庫用戶,只爲測試現在:
public class User {
String userId;
String userEmail;
Boolean userHasInterest;
String eventId;
public String getUserId() {
return userId;
}
public String getUserEmail() {
return userEmail;
}
public Boolean getUserHasInterest() {
return userHasInterest;
}
public String getEventId() {
return eventId;
}
public User(String userId, String eventId, String userEmail, Boolean userHasInterest) {
this.userId = userId;
this.userEmail = userEmail;
this.userHasInterest = userHasInterest;
this.eventId = eventId;
}
與方法,只是挑另一個活動的事件的關鍵:
public String getKeyEvent(){
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String key = bundle.getString("Evento");
return key;
}
添加數據庫結構並說明getKeyEvents()是什麼 –
此代碼是否工作?您在範圍內有'dataSnapshot'重疊的值 –