Breaking News

How to Retrieve Value from Firebase Realtime Database

How to Retrieve Value from Firebase Realtime Database



  • Create and Insert in Database Click Here
  • Update data in Database Click Here
  • Delete Data in Database Click Here

To Retrieve value from Firebase Realtime Database and show it on a ListView.

Then we need a Custom_List layout with 3 TextView to show value and an adapter class to set up our ListView.

First, we create a list view in our activity_main.xml

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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:orientation="vertical"  
   tools:context=".MainActivity">  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Roll No"  
     android:id="@+id/student_roll"/>  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Name"  
     android:id="@+id/student_name"/>  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Std"  
     android:id="@+id/student_std"/>  
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Add Student"  
     android:id="@+id/add_btn"/>  
   <ListView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/student_listview"/>  
 </LinearLayout>  

Then we create a custom layout with 3 Text Views to inflate on our list view.

student_custom_list.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical">  
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:textSize="16sp"  
     android:id="@+id/roll_textview"/>  
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:textSize="16sp"  
     android:id="@+id/name_textview"/>  
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:textSize="16sp"  
     android:id="@+id/std_textview"/>  
 </LinearLayout>  

Then we create an adapter class to set up our ListView

Student_ListAdaptor.java
 package studio.harpreet.firebasedatabase;  
 import android.app.Activity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.ArrayAdapter;  
 import android.widget.TextView;  
 import androidx.annotation.NonNull;  
 import androidx.annotation.Nullable;  
 import java.util.List;  
 public class Student_ListAdaptor extends ArrayAdapter<StudentModel> {  
   private Activity context;  
   private List<StudentModel> studentModelList;  
   public Student_ListAdaptor(Activity context, List<StudentModel> studentModelList)  
   {  
     super(context,R.layout.student_custom_list,studentModelList);  
     this.context = context;  
     this.studentModelList = studentModelList;  
   }  
   @NonNull  
   @Override  
   public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {  
     LayoutInflater inflater = context.getLayoutInflater();  
     View ListItem = inflater.inflate(R.layout.student_custom_list,null,true);  
     TextView rollTextView = ListItem.findViewById(R.id.roll_textview);  
     TextView nameTextView = ListItem.findViewById(R.id.name_textview);  
     TextView stdTextView = ListItem.findViewById(R.id.std_textview);  
     StudentModel studentModel = studentModelList.get(position);  
     rollTextView.setText("Roll No. "+studentModel.getRollno());  
     nameTextView.setText("Name: "+studentModel.getName());  
     stdTextView.setText("Std: "+studentModel.getStd());  
     return ListItem;  
   }  
 }  

Here we set values on our text view, now we can code to retrieve data from Firebase  Realtime Database in our MainActivity.java.

In Our MainActivity, we use value event listener and get all data in an ArrayList and show that ArrayList on our ListView.

MainActivity.java
 package studio.harpreet.firebasedatabase;  
 import androidx.annotation.NonNull;  
 import androidx.appcompat.app.AlertDialog;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.content.DialogInterface;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.LinearLayout;  
 import android.widget.ListView;  
 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.ValueEventListener;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class MainActivity extends AppCompatActivity {  
   DatabaseReference databasestudent;  
   ListView studentListView;  
   List<StudentModel > studentlist;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     studentListView = findViewById(R.id.student_listview);  
     studentlist = new ArrayList<>();  
     databasestudent = FirebaseDatabase.getInstance().getReference("Student");  
   }  
   @Override  
   protected void onStart() {  
     super.onStart();  
     databasestudent.addValueEventListener(new ValueEventListener() {  
       @Override  
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {  
         studentlist.clear();  // because everytime when data updates in your firebase database it creates the list with updated items  
                     // so to avoid duplicate fields we clear the list everytime  
         if(dataSnapshot.exists())  
         {  
           for(DataSnapshot studentsnapshot : dataSnapshot.getChildren())  
           {  
             StudentModel studentModel = studentsnapshot.getValue(StudentModel.class);  
             studentlist.add(studentModel);  
           }  
           Student_ListAdaptor adaptor = new Student_ListAdaptor(MainActivity.this,studentlist);  
           studentListView.setAdapter(adaptor);  
         }  
       }  
       @Override  
       public void onCancelled(@NonNull DatabaseError databaseError) {  
       }  
     });  
   }  
 }  

Now you can run your app and see the added items in ListView.


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments