I want to retrieve data from my Realtime Database server so that I can update values based on the reg-id (stored this as a child string, not as Auth) I have saved for the user.
I have done this till now
private void updateDatatoFirebase(String regid, String doctor_name, String city,String speciality) {
Map<String, Object> update=new HashMap<>();
update.put("doctor",doctor_name);
update.put("city",city);
update.put("speciality",speciality);
DatabaseReference dbref=FirebaseDatabase.getInstance().getReference();
dbref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()){
key = snapshot.getKey();
if(checkid.equalsIgnoreCase(regid))
{
dbref.updateChildren(update);
Toast.makeText(need_help.this, "Data in Database updated", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(need_help.this, "No such id found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(need_help.this, "Fail to add data " error, Toast.LENGTH_SHORT).show();
}
});
}
to update but it won't update and I have saved data like this
private void addDatatoFirebase(String regid, String doctor_name, String city,String speciality) {
root=firebaseDatabase.getReference().child("RegId").child(regid);
HashMap<String, String> usermap=new HashMap<>();
usermap.put("doctor",doctor_name);
usermap.put("city",city);
usermap.put("speciality",speciality);
root.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
root.push().setValue(usermap);
Toast.makeText(need_help.this, "Data in Database updated", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(need_help.this, "Fail to add data " error, Toast.LENGTH_SHORT).show();
}
});
CodePudding user response:
To be able to update all children under a particular node, for instance, 1234
, you have to pass to the following method, 1234
as the first argument:
private void updateDatatoFirebase(String regid, String doctor_name, String city,String speciality) {
Map<String, Object> update=new HashMap<>();
update.put("doctor",doctor_name);
update.put("city",city);
update.put("speciality",speciality);
DatabaseReference dbref=FirebaseDatabase.getInstance().getReference();
dbref.child("RegId").child(regid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()){
ds.getRef().updateChildren(update).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(need_help.this, "Data in Database updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(need_help.this, "Failed with an error: " task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(need_help.this, "Fail to add data " error, Toast.LENGTH_SHORT).show();
}
});
}
Things to notice:
- I have added two extra calls
.child("RegId")
and.child(regid)
call right after the root reference. - I have used the
ds
to callgetRef()
and then.updateChildren(update)
. - I have attached a listener to see if something goes wrong when updating the children under
1234
.