Android RecyclerView → Android Realm RecyclerView – Camposha

Android RecyclerView

Android RecyclerView

Android RecyclerView

Android Realm RecyclerView

Android Realm RecyclerView tutorial

Android Realm RecyclerView Tutorials and Examples.


Android Realm – RecyclerView – Save,Retrieve,Show

Today we look at how to work with Realm database alongside a RecyclerView component with CardView.The fisrt thing we do is save/write data to Realm database in our android application.We do this from an edittext.We display to the user a material edittexts inside an input dialog.

He enters the data,clicks save and then we save the data. We also retrieve data from the Realm database and fill an arraylist.This arraylist we then pass t our RecyclerView adapter and bind it as our dataset to the RecyclerView.The RecyclerView consists of CardViews.


  • Save Data to Realm database.
  • Retrieve data from Realm database
  • Bind Realm data to RecyclerView.

Android realm RecyclerView Project Structure

Source Code

The first thing is to add our Realm dependency via our build.gradle,app level.You may want to make sure you have a later version than I used here .

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ''
    compile ''
    compile ''
    compile 'io.realm:realm-android:0.82.1'

Then we can define our data object,our model class as below :

package com.tutorials.hp.recyclerviewrealm.m_Realm;

import io.realm.RealmObject;

public class Spacecraft extends RealmObject {

    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

Now you can see that we derive from RealmObject.This model class shall correspond to our database table.

Lets see our RealmHelper,the class that shall be responsible for all CRUD operations in our project.Here we save to Realm database,retrieve data and fill an arraylist.

package com.tutorials.hp.recyclerviewrealm.m_Realm;

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmResults;

public class RealmHelper {

    Realm realm;

    public RealmHelper(Realm realm) {
        this.realm = realm;

    public void save(final Spacecraft spacecraft)
        realm.executeTransaction(new Realm.Transaction() {
            public void execute(Realm realm) {

                Spacecraft s=realm.copyToRealm(spacecraft);



    public ArrayList<String> retrieve()
        ArrayList<String> spacecraftNames=new ArrayList<>();
        RealmResults<Spacecraft> spacecrafts=realm.where(Spacecraft.class).findAll();

        for(Spacecraft s:spacecrafts)

        return spacecraftNames;

Android Realm save data A

Next we are using a RecyclerView so we need an adapter :

package com.tutorials.hp.recyclerviewrealm.m_UI;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tutorials.hp.recyclerviewrealm.R;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    Context c;
    ArrayList<String> spacecrafts;

    public MyAdapter(Context c, ArrayList<String> spacecrafts) {
        this.c = c;
        this.spacecrafts = spacecrafts;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
        return new MyViewHolder(v);

    public void onBindViewHolder(MyViewHolder holder, int position) {


    public int getItemCount() {
        return spacecrafts.size();

The purpose of our Adapter class is to receive a List of data and bind it to our RecyclerView.We have a recyclerView.ViewHolder subclass that you can view in the full source code reference download at the top of this page.

Lets have a look at our MainActivity class.First we are going to show our input dialog that has the edittexts to input data and button to save.

We shall pass the typed data to our RealmHelper class for it to be saved to database. We are also going to bind our data to our RecyclerView via an adapter.

Android Realm RecyclerView


Look we’ve looked at how to save data to Realm database,read that data and bind to a recyclerView.That’s it for today.The full source code can be downloaded above.Download it,extract it and import to your android studio. Cheers.


If you prefer more step by step explanations then you can check the video tutorial below.Or watch the demo here.


No. Location Link
1. GitHub Direct Download)
2. GitHub Browse
3. YouTube Video Tutorial
4. YouTube Our YouTube Channel

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Us