我已使用hashmap将用户数据保存到firebase数据库。 用户的数据按其ID排序。
保存代码是:
emailauth = FirebaseAuth.getInstance();
donorid= emailauth.getCurrentUser().getUid();
mrefrnce= FirebaseDatabase.getInstance().getReference().child("Users").child("Donor").child(donorid);
HashMap hmap= new HashMap();
hmap.put("user_name",usern);
hmap.put("bloodgroup",bld);
hmap.put("user_address",address);
hmap.put("user_type","Blood Donor");
mrefrnce.updateChildren(hmap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Toast.makeText(MakeDonorProfile.this,"Data has been Saved",Toast.LENGTH_SHORT).show();
progress.dismiss();
Intent intent= new Intent(MakeDonorProfile.this, PlacePick.class);
startActivity(intent);
}
}
});
在上面的代码中,我按照捐赠者ID
保存了数据这是检索代码
DatabaseReference donordRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Donor");
donordRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long size = dataSnapshot.getChildrenCount();
for (DataSnapshot child : dataSnapshot.getChildren()) {
getDonorData donordata = dataSnapshot.getValue(getDonorData.class);
String latitude = child.child("latitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
String bloodgroup = child.child("bloodgroup").getValue().toString();
String do_address = child.child("user_address").getValue().toString();
String donarname = child.child("user_name").getValue().toString();
double loclatitude = Double.parseDouble(latitude);
double loclongitude = Double.parseDouble(longitude);
LatLng cod = new LatLng(loclatitude, loclongitude);
mMap.addMarker(new MarkerOptions().position(cod).title( donarname).snippet("Blood Group: "+bloodgroup+"\n"+"Address: "+do_address));
mMap.setInfoWindowAdapter(new InfoWindowAdapter(SearchActivity.this));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
现在在检索代码时,我希望获得其他数据的捐赠者ID。
如何在DataSnapshot for循环中为所有捐赠者获取此ID?
数据库图片为 click database picture
提前致谢
此致
Ahsan Sherazi
答案 0 :(得分:0)
我认为这可能有所帮助。
DatabaseReference databaseRef = databaseReference.child("Donor");
databaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
Log.d("GetDonorId","DataSnapshot :"+dataSnapshot1.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});