I am trying to use a textview to display the latest data from the database…
- User
I am trying to use a textview to display the latest data from the database
textresult.java
package com.example.profile;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.Query; import com.google.firebase.database.ValueEventListener;
public class textresult extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("predict"); Query query = myRef.orderByChild("timestamp").limitToLast(1); query.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { Log.d("FirebaseDebug", "onDataChange: Data received"); // Add this line for (DataSnapshot snapshot : dataSnapshot.getChildren()) { // Get the "soreness_label", "gsr_data" and "emg_data" from the latest entry String latestSorenessLabel = snapshot.child("soreness_label").getValue(String.class); // Combine values to be displayed String combinedValues = "Soreness Label: " + latestSorenessLabel ; // Display combined values in the TextView TextView textViewData = findViewById(R.id.textViewData); textViewData.setText(combinedValues); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { Log.d("FirebaseDebug", "onCancelled: Error encountered"); // Add this line Log.w("Error", "Failed to read value.", databaseError.toException()); } }); }}
activity_main.java
<androidx.drawerlayout.widget.DrawerLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:id='@+id/drawer' tools:context='.MainActivity'>
<LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:background='@drawable/bg' android:orientation='vertical'> <include layout='@layout/main_toolbar' /> <TextView android:layout_width='match_parent' android:layout_height='match_parent' android:text='' android:textSize='52sp' android:textStyle='bold' android:textColor='@color/black' android:gravity='center' android:id='@+id/textViewData' /> </LinearLayout> <RelativeLayout android:layout_width="300dp" android:layout_height="match_parent" android:background="@color/white" android:layout_gravity="start"> <include layout = "@layout/main_navigation_drawer" /> </RelativeLayout> - Assistant
It seems that you are trying to display the latest data from the database in a TextView. The code you have provided for textresult.java and activity_main.xml should work fine, assuming that your Firebase database is correctly set up, and you have the data stored under the "predict" reference.
- User
does this textview update?
- Assistant
Yes, the TextView will update whenever there is a change in the data.
In your code for
textresult.java, you are usingaddValueEventListenermethod which sets a listener for any changes in the data under the specified reference (in this case, thepredictreference in your Firebase Realtime Database).Inside the
onDataChangemethod, you update the TextView with the latest data:TextView textViewData = findViewById(R.id.textViewData); textViewData.setText(combinedValues);So, whenever there is a change in the data (e.g., a new entry is added, an existing entry is modified or deleted), the
onDataChangemethod will be triggered, and your TextView will be updated with the latest data.Keep in mind that this update is real-time and will automatically reflect in the TextView as the data changes in the database.