Reputation: 306
I'm working on Geo location Android application which is using Firebase for back end and database.
Here is my structure for the data in my database.
Here is mu User.java class.
@IgnoreExtraProperties
public class User {
public String fullName;
public String email;
public String longitude;
public String latitude;
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public User() {
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
What I want to do.
How I'm doing it now?
Here is the code sniped for it.
dbRef = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
User user = new User();
String currentUserId = mAuth.getCurrentUser().getUid();
user.setLatitude(String.valueOf(userLocation.latitude));
user.setLongitude(String.valueOf(userLocation.longitude));
dbRef.child("users").child(currentUserId).setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.i("Info", "updateUser:success");
} else {
Log.i("Info", "updateUser:failure");
}
}
});
What is wrong?
Upvotes: 0
Views: 1669
Reputation: 12005
The problem is that you're not setting fullName and email when setting latitude and longitude, then those field are null. setValue()
will erase the old object and add the new one with the fields null. You should be doing an updateChildren
instead of setValue
.
Map<String, Object> latLong = new HashMap<>();
latLong.put("latitude", "123");
latLong.put("longitude", "123");
dbRef.child("users").child(currentUserId).updateChildren(latLong).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.i("Info", "updateUser:success");
} else {
Log.i("Info", "updateUser:failure");
}
}
});
You can keep using your code if you set the email and fullName alongside with latitude and longitude.
Upvotes: 1