Reputation: 137
patient=mFirebaseAuth.getCurrentUser();
databaseReference=FirebaseDatabase.getInstance().getReference("Patients");
Query query= databaseReference.orderByChild(patient.getUid());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
deviceId= String.valueOf(ds.child("devID").getValue());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
databaseReferences= FirebaseDatabase.getInstance().getReference(deviceId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
mbt= String.valueOf(ds.child("BT").getValue());
Log.d("myTag", "This is my message"+mbt);
Toast.makeText(getApplicationContext(),"Hi"+mbt,Toast.LENGTH_SHORT).show();
bt.setText(mbt);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Log
D/myTag: This is my message98.9
D/myTag: This is my messagenull
Here, in this, at first in Log and as well as in toast, I'm getting the actual value for some moments and after a while its changing to null. and my console and code screenshot is,
My firebase database structure is...
{
"123456" : {
"BT" : 98.9,
"DBP" : 80,
"HR" : 90,
"SBP" : 125.5
},
"Patients" : {
"I6nb3CB52ARmWtfpA5ZRpLjiNbx1" : {
"age" : "22",
"devID" : "123456",
"eillness" : "Corona",
"email" : "[email protected]",
"fname" : "asdf",
"lname" : "jkl"
}
}
}
Here, in this, at first in Log and as well as in toast, and texview also I'm getting the actual value for some moments and after a while its changing to null.
Upvotes: 2
Views: 551
Reputation: 1091
I am not sure about using the same Object DatabaseReference and Reference change for them at the same time, but to avoid errors I created an object for each of them And you check the ID inside OnDataChange if it doesn't exist, it will take you to onCancelled and do the appropriate procedure in this .
You can check the code below:
patient=mFirebaseAuth.getCurrentUser();
refPatients=FirebaseDatabase.getInstance().getReference("Patients");
refPatients.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// I have modified this line and checked it inside OnDataChange if it exists or not.
if(dataSnapshot.hasChild(Objects.requireNonNull(patient.getUid()))){
for (DataSnapshot ds : dataSnapshot.child(Objects.requireNonNull(patient.getUid())).getChildren()){
deviceId = String.valueOf(ds.child("devID").getValue());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//You can make sure to undo correctly and read the error if no value is retrieved from Logcat
Log.d("ErrorRef",databaseError.getMessage();
);
}
});
refDeviceId=FirebaseDatabase.getInstance().getReference(deviceId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("BT")){
Stringmbt = String.valueOf(dataSnapshot.child("BT").getValue());
bt.setText(mbt);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 1
Reputation: 126
Try to use addListenerForSingleValueEvent instead of addValueEventListener.
Upvotes: 0
Reputation: 1542
Your .addValueEventListener(listener)
works async. You cannot return something now that hasn't been loaded yet. So you will return your field first which is null and after the call is done, your listener get called. But your field is already returned. You can see this by placing a few log statements:
So to fix this, whether you write your own listener like:
interface MyCallback{
void onSuccess(String BT)
}
and do:
public void getData(MyCallback callback) {
databaseReferences= FirebaseDatabase.getInstance().getReference(deviceId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
mbt= String.valueOf(ds.child("BT").getValue());
Log.d("myTag", "This is my message"+mbt);
Toast.makeText(getApplicationContext(),"Hi"+mbt,Toast.LENGTH_SHORT).show();
bt.setText(mbt);
}
callback.onSuccess(String BT);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Refer Setting Singleton property value in Firebase Listener
Upvotes: 0
Reputation: 4035
According to your database, you should be extracting values like this without loop:
databaseReferences= FirebaseDatabase.getInstance().getReference(deviceId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//if the value is double use : (double value = dataSnapshot.child("BT").getValue(Double.class);)
float value = dataSnapshot.child("BT").getValue(Float.class);
mbt= String.valueOf(value);
Log.d("myTag", "This is my message"+mbt);
Toast.makeText(getApplicationContext(),"Hi"+mbt,Toast.LENGTH_SHORT).show();
bt.setText(mbt);
}
.......
Upvotes: 0