When I debug this code, in databaseUser dataSnapshot has the values from User in database, but in the object user1
, did not add, it's all null. And I don't understand, because in the object event receives values normally.
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);
}
}
});
My Database User, only for test for now:
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;
}
And the method, just to pick the key of the event of another activity :
public String getKeyEvent(){
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String key = bundle.getString("Evento");
return key;
}
When you execute a query against the Firebase Database, there will potentially be multiple results. By attaching a ValueListener
you get those results in one snapshot, so the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your code fails to take this "list of results" into account. It's easy to fix by looping over the DataSnapshot.getChildren()
:
databaseUser.orderByChild("userEmail").equalTo(user.getEmail()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
User user1 = userSnapshot.getValue(User.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With