Sunday, 12 March 2017

How to work with Firebase Recylerview


hai friends in this tutorial we will learn how to use firebase recyclerview ,this is one of the famous and important ui component in android it makes our work easier

target of our application 


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 dependencies in our project under project structure build.gradle(Module: app) file in module section

  1. compile 'com.google.firebase:firebase-database:10.0.0'
  2. compile 'com.firebaseui:firebase-ui-database:0.6.2'
  3. compile 'com.android.support:recyclerview-v7:24.2.1'
  4. compile 'com.android.support:cardview-v7:24.2.1'
  5. compile 'com.squareup.picasso:picasso:2.5.2'


uses of above dependencies

  • firebase database dependency used to uses the database feature of firebase 
  • firebase ui dependency used to working with firebase recyclerview
  • recyclerview dependency used to working with inbuilt recyclerview of android
  • cardview dependency used to working with cardview 
  • picasso dependency used to set the images into imageview from network urls in easier way

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_main"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   >  
   <android.support.v7.widget.RecyclerView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:id="@+id/recyclerview">  
   </android.support.v7.widget.RecyclerView>  
 </RelativeLayout>  
  • create another XML file(i.e individual_row.xml) under res/layout directory
  • then make that xml file to look like this

individual_row.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent" android:layout_height="match_parent">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:orientation="vertical"  
     android:layout_height="wrap_content">  
     <ImageView  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:src="@mipmap/ic_launcher"  
       android:id="@+id/image"/>  
     <TextView  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:text="title"  
       android:id="@+id/title"/>  
     <TextView  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:text="description"  
       android:id="@+id/description"/>  
   </LinearLayout>  
 </android.support.v7.widget.CardView>  
  • finally we will get user interface like this

individual_rowactivity_main



  • some times you will get activity_main show like this due to some android studio errors but it will works fine
  • next we will manually add some data to our database section like below

  • make the MainActivity.java file like this

MainActivity.java

 package com.example.mahesh.test;  
 import android.content.Context;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.support.v7.widget.LinearLayoutManager;  
 import android.support.v7.widget.RecyclerView;  
 import android.view.View;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import com.firebase.ui.database.FirebaseRecyclerAdapter;  
 import com.google.firebase.database.DatabaseReference;  
 import com.google.firebase.database.FirebaseDatabase;  
 import com.squareup.picasso.Picasso;  
 public class MainActivity extends AppCompatActivity {  
   private RecyclerView recyclerView;  
   private DatabaseReference myref;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     recyclerView=(RecyclerView)findViewById(R.id.recyclerview);  
     recyclerView.setHasFixedSize(true);  
     recyclerView.setLayoutManager(new LinearLayoutManager(this));  
     myref= FirebaseDatabase.getInstance().getReference().child("/blog");  
     FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(  
         Blog.class,  
         R.layout.individual_row,  
         BlogViewHolder.class,  
         myref  
     ) {  
       @Override  
       protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {  
         viewHolder.setTitle(model.getTitle());  
         viewHolder.setDescription(model.getDescription());  
         viewHolder.setImage(model.getImage());  
       }  
     };  
   recyclerView.setAdapter(recyclerAdapter);  
   }  
   public static class BlogViewHolder extends RecyclerView.ViewHolder {  
     View mView;  
     TextView textView_title;  
     TextView textView_decription;  
     ImageView imageView;  
     public BlogViewHolder(View itemView) {  
       super(itemView);  
       mView=itemView;  
       textView_title = (TextView)itemView.findViewById(R.id.title);  
       textView_decription = (TextView) itemView.findViewById(R.id.description);  
       imageView=(ImageView)itemView.findViewById(R.id.image);  
     }  
     public void setTitle(String title)  
     {  
       textView_title.setText(title+"");  
     }  
     public void setDescription(String description)  
     {  
       textView_decription.setText(description);  
     }  
     public void setImage(String image)  
     {  
       Picasso.with(mView.getContext())  
           .load(image)  
           .into(imageView);  
     }  
   }  
 }  

  • create another new java file(i.e Blog) under our application package
  • makes that file look like this

Blog.java

 package com.example.mahesh.test;  
 /**  
  * Created by uma maesh on 12-03-17.  
  */  
 public class Blog {  
   private String title,description,image;  
   public Blog() {  
   }  
   public Blog(String title, String description, String image) {  
     this.title = title;  
     this.description = description;  
     this.image = image;  
   }  
   public String getTitle() {  
     return title;  
   }  
   public void setTitle(String title) {  
     this.title = title;  
   }  
   public String getDescription() {  
     return description;  
   }  
   public void setDescription(String description) {  
     this.description = description;  
   }  
   public String getImage() {  
     return image;  
   }  
   public void setImage(String image) {  
     this.image = image;  
   }  
 }  

Note  : if you are not implementing authentication(Login system) for your                           application (like me) you follow the following steps  

  • click here
  • it will automatically redirects to firebase console page, there you will find your firebase projects
  • then select your project and click database section
  • under database section click Rules tab for changing the rules
  • change the your default rules like below
 {  
  "rules": {  
   ".read": "true",  
   ".write": "true"  
  }  
 }  

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

69 comments

how to implement this code in fragment ????

@prakash shahi for fragment pls see this code ->https://drive.google.com/open?id=0B_6l6OGwOMcQRmEwZW5qRVpWb2s

Hi ,
The code was extremely helpful but this gives only one group values under one object but how to code if i want to retrieve all objects under Blog.Kindly help me

Please tell me how to add data to firebase data connection, it is some what clear

Hey i am facing problem with your code while i run
it gives me error saying
Error35, 34) error: cannot access zzbql
class file for com.google.android.gms.internal.zzbql not found

This is showing all objects inside blog object not single object

Pls see this video https://youtu.be/IYVb42sUh4w

bayya please give me ur contact info....please...understand

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

When I tried to implement recyclerview it shows a run time error like this.I did n't find any problem in mycode.My activity close at the time of errorand goes to mainactivity.I am Using fragment.Plz Help...Thanks for adavanced Help


java.lang.NoSuchMethodError: android.os.Binder#execTransact(int,int,int,int)#exact
at de.robv.android.xposed.XposedHelpers.findMethodExact(XposedHelpers.java:339)
at de.robv.android.xposed.XposedHelpers.findAndHookMethod(XposedHelpers.java:176)
at de.robv.android.xposed.XposedHelpers.findAndHookMethod(XposedHelpers.java:251)
at com.phoneinfo.changerpro.hooks.g.a(Unknown Source)
at com.phoneinfo.changerpro.hooks.MainHook.handleLoadPackage(Unknown Source)
at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(IXposedHookLoadPackage.java:34)
at de.robv.android.xposed.callbacks.XC_LoadPackage.call(XC_LoadPackage.java:61)
at de.robv.android.xposed.callbacks.XCallback.callAll(XCallback.java:106)
at de.robv.android.xposed.XposedBridge$1.beforeHookedMethod(XposedBridge.java:234)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:1550)
at android.app.ActivityThread.handleBindApplication()
at android.app.ActivityThread.access$1600(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5296)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:163)

how can get the listview items clickable?
can you help me please

how to make the items in recycler view clickable??

Apk build but nothing showing
Internet permissions are okay in manifest file very thing is fine but data isn't showing

Hi, how can we do infinite pagination/scrolling with FirebaseRecyclerAdapter. pls help!! My email mahfuzrubd@gmail.com. Thanks in advance.!

it show only one object one the top of the database

How to add such data in firebase database

What is Error----------------
public class Main2Activity extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference myref;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myref= FirebaseDatabase.getInstance().getReference().child("mainObject");


FirebaseRecyclerAdapter recyclerAdapter=new FirebaseRecyclerAdapter(
Person.class,
R.layout.list_item,
BlogViewHolder.class,
myref
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Person model, int position) {
viewHolder.setName(model.getName());
viewHolder.setEmail(model.getEmail());
viewHolder.setImage(model.getImageUri());
}
};
recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView userName;
TextView Email;
ImageView imageView;

public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
userName = (TextView) itemView.findViewById(R.id.getName);
Email = (TextView) itemView.findViewById(R.id.getEmail);
imageView = (ImageView) itemView.findViewById(R.id.getImage);
}

public void setName(String name) {
userName.setText(name);
}

public void setEmail(String email) {
Email.setText(email);
}

public void setImage(String imageUri) {
Picasso.with(mView.getContext()).load(imageUri).into(imageView);
}
}
}

Can't convert object of type java.lang.String to type com.sports.mysports.Blog

Brother,

it showing only 1 record, how to fetch all the records from Blog?
how to place search quries?

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

This comment has been removed by the author.

Warning!! SPAM has been detected!

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

Warning!! SPAM has been detected!

This comment has been removed by the author.

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

This comment has been removed by the author.

Warning!! SPAM has been detected!

This comment has been removed by the author.
This comment has been removed by the author.

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Finish the Selenium Training in Chennai from Infycle Technologies, the best software training institute in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Java, Hadoop, Big Data, Android, and iOS Development, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

This comment has been removed by the author.

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!

Warning!! SPAM has been detected!


Emoticon Emoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)