Sasindu Usgalhewa
Sasindu Usgalhewa

Reputation: 33

Firebase Realtime Database not displayed when retriving to textviews in Android

onCreate() method is never executed. I just want to take the below data to 4 TextViews. "Detail" is the model class. No errors are shown when running the app. view_temp is an activity. this is firebase realtime db

this is the java class

`public class view_temp extends AppCompatActivity {
    private view_temp viewTemp;
    public Detail detail;
    private TextView roomtemp, roomhuminity, bodypulse, bodytemp;
    private DatabaseReference mDatabase;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_temp);

        mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");

        roomtemp = (TextView) findViewById(R.id.txt_room_temp);
        roomhuminity = (TextView) findViewById(R.id.txt_huminity_temp);
        bodypulse = (TextView) findViewById(R.id.txt_body_pulse);
        bodytemp = (TextView) findViewById(R.id.txt_body_temp);

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    detail = postSnapshot.getValue(Detail.class);

                    String rtemp = detail.getRoomtemp();
                    String rhumidity = detail.getRoomhumidity();
                    String bpulse = detail.getBodypulse();
                    String btemp = detail.getBodytemp();

                    roomtemp.setText(rtemp);
                    roomhuminity.setText(rhumidity);
                    bodypulse.setText(bpulse);
                    bodytemp.setText(btemp);
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getMessage());
            }
        });
    }


}`

Upvotes: 3

Views: 52

Answers (2)

Saiful Islam
Saiful Islam

Reputation: 1269

Just change your line

mDatabase = FirebaseDatabase.getInstance().getReference("heat-stroke-device");

to

mDatabase = FirebaseDatabase.getInstance().getReference();

Note: You do not need to put your database name into reference.

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

There is no need to add the name of the project in the getReference() method. To get those names and the corresponding values correctly, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.getKey();
            double value = ds.getValue(Double.class);
            Log.d("TAG", name "/" + value);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
rootRef.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be:

RoomHumi/86.0
RoomTemp/30.8
bodyPulse/126.0
bodyTemp/29.0

If the keys are always fixed, then simply use:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        double roomHumi = dataSnapshot.child("RoomHumi").getValue(Double.class);
        Log.d("TAG", "RoomHumi" "/" + valueroomHumi);
        double roomTemp = dataSnapshot.child("RoomTemp").getValue(Double.class);
        Log.d("TAG", "RoomTemp" "/" + roomTemp);
        double bodyPulse = dataSnapshot.child("bodyPulse").getValue(Double.class);
        Log.d("TAG", "bodyPulse" "/" + bodyPulse);
        double bodyTemp = dataSnapshot.child("bodyTemp").getValue(Double.class);
        Log.d("TAG", "bodyTemp" "/" + bodyTemp);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
rootRef.addListenerForSingleValueEvent(valueEventListener);

And you'll have the same result in the logcat.

Upvotes: 2

Related Questions