How to read a single data row from Android Firebase Database

In this android programming source code example, we are going to read a single data row from Android Firebase Database.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to read a single data row from Android Firebase Database.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:background="#CACACA">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:textSize="20sp"
        android:layout_marginTop="90dp"/>

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="2sp"
        android:layout_below="@id/title"
        android:background="@color/colorAccent"/>

    <TextView
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="body"
        android:textSize="30sp"
        android:layout_below="@id/view1"/>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

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 FirebaseActivity12 extends AppCompatActivity {
    private TextView title;
    private TextView body;
    private DatabaseReference mDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firebase12);
        title = (TextView)findViewById(R.id.title);
        body = (TextView)findViewById(R.id.body);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("AndroidView");
        Query query = mDatabase.orderByChild("title");
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot readData: dataSnapshot.getChildren()){
                        title.setText(readData.child("title").getValue().toString());
                        body.setText(readData.child("content").getValue().toString());
                    }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(FirebaseActivity12.this,databaseError.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

app/build.gradle

implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-database:17.0.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.