Tuesday, 14 March 2017

Firebase Real time database with ListView

Tags

hai friends in this tutorial we will learn how to use firebase listview ,this is used to display the content of firebase realtime database in quick manner


Lets start our work

  • create a new android project in android studio
  • setup the firebase with our android project (if u don't know about please see the below video)

  • add the following dependency in our project under project structure build.gradle(Module: app) file in module section
 compile 'com.google.firebase:firebase-database:9.8.0'  

uses of above dependency

  • firebase database dependency used to uses the database feature of firebase 

create User Interface for our Application

  • make our activity_main.xml file to below  

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/activity_main2"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context="com.example.mahesh.firebasesample.Main2Activity">  
   <ListView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:id="@+id/listview"  
     android:layout_alignParentBottom="true"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentStart="true"  
     android:layout_marginBottom="11dp" />  
 </RelativeLayout>  
  • finally we get user interface like this
  • make the MainActivity.java file like this

MainActivity.java

 package com.example.mahesh.firebasesample;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.widget.ArrayAdapter;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 import com.google.firebase.database.ChildEventListener;  
 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.Map;  
 public class Main2Activity extends AppCompatActivity {  
   DatabaseReference dref;  
   ListView listview;  
   ArrayList<String> list=new ArrayList<>();  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     listview=(ListView)findViewById(R.id.listview);  
     final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list);  
     listview.setAdapter(adapter);  
     dref=FirebaseDatabase.getInstance().getReference();  
     dref.addChildEventListener(new ChildEventListener() {  
       @Override  
       public void onChildAdded(DataSnapshot dataSnapshot, String s) {  
         list.add(dataSnapshot.getValue(String.class));  
         adapter.notifyDataSetChanged();  
       }  
       @Override  
       public void onChildChanged(DataSnapshot dataSnapshot, String s) {  
       }  
       @Override  
       public void onChildRemoved(DataSnapshot dataSnapshot) {  
         list.remove(dataSnapshot.getValue(String.class));  
         adapter.notifyDataSetChanged();  
       }  
       @Override  
       public void onChildMoved(DataSnapshot dataSnapshot, String s) {  
       }  
       @Override  
       public void onCancelled(DatabaseError databaseError) {  
       }  
     });  
   }  
 }  

Thanks for seeing our blog if you have any doubts please comment below i will come with better solution


Emoticon Emoticon