[英]How to retrieve data from Firebase in android RealTimeDatabase to update on the basis of regid
我想从我的实时数据库服务器检索数据 这样我就可以根据我为用户保存的 reg-id(将其存储为子字符串,而不是 Auth)更新值。
到目前为止我已经这样做了
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();
}
});
}
更新但不会更新我已经保存了这样的数据
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();
}
});
为了能够更新特定节点下的所有子节点,例如1234
,您必须将1234
作为第一个参数传递给以下方法:
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();
}
});
}
注意事项:
.child("RegId")
和.child(regid)
调用。ds
调用getRef()
然后.updateChildren(update)
。1234
以下的子项时是否出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.