Reputation: 184
I have number of Medicines corresponding to each patient unique key. i am facing trouble to retrieve all of medicine data of the patient Here is my database structure.
{
"Medicine" : {
"-LnRyr-3szcVYVtr_d4m" : {
"Med1" : {
"dosage" : "1+1+1",
"medname" : "Panadol",
"time" : "After Every Meal"
},
"Med2" : {
"Mmedname" : "Raisik",
"med2dosage" : "1+1+1",
"med2time" : "after every meal 1 week"
}
}
}
}
Code
databaseReference = FirebaseDatabase.getInstance().getReference("Medidine");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot requestSnapshot: dataSnapshot.getChildren()) {
DataSnapshot ds = requestSnapshot.child("Med1");
for (DataSnapshot medicinesnapshot: ds.getChildren()) {
String MedicineName = medicinesnapshot.child("medname").getValue(String.class);
String MedDosage = medicinesnapshot.child("dosage").getValue(String.class);
String medtime = medicinesnapshot.child("time").getValue(String.class);
marray.add(MedicineName+MedDosage+medtime);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 1
Views: 110
Reputation: 7193
For a patient with patientId=-LnRyr-3szcVYVtr_d4m;
then you can get all medicines related to that patient like this
String patientId="-LnRyr-3szcVYVtr_d4m";
FirebaseDatabase.getInstance().getReference().child("Medicine").child(patientId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
MedicineData medicineData = snapshot.getValue(MedicineData.class);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 1