Realm database working with Android.

Posted by

Introductions

SQLine is popular database for past few years. Which lightweight database will be use in Android platform. Nowadays Realm database modern mobile platform. they are working the both Android and IOS platform.

In this tutorial I will guide lasted version 5.3.0

You can go Realm officially page in order to learn more about Realm database.

In my guide. I will overview and short code let to you visualize. How to Realm database can work in Android.

Download Now

 

  1. Creating project
    • From Android studio tool choose (File->New-New Project and then fill up…)
  2. Initialize Realm and Widget.
    • Modifying build.gradle at root folder
    • Adding Realm class path.

       
    • From Root project -> app -> build.gradle

       
    • In this tutorial You will be using butterknife in to order to map id from xml to Activities. It will save more your time.
  3. Building infrastructure for project.
    • As previous tutorial. You will create 3 packages (model,ui,common) each package will have contain classes or components.
    • First of all you need to initialize Realm into Application.
    • Creating class name MyApplication

       
    • And then you will move to manifest in order to adding android:name=”MyApplication”

       
    • Creating RealmController into common package. this class will be handler insert update, delete etc…

       
    • Creating BaseAdapter class into adapter folder

       
    • Creating BaseHolder class into adapter package

    •  
    • Creating DividerItemDecoration class into adapter package

       
    • Finally RecyclerViewAdapterWrapper class into adapter package .

       
  4. Creating class into model package.
    • Let realm can work such as insert, update, delete directly specific classes.
    • You need to create TaskManager Class into model package.

       
  5. Creating activity and xml
    • Modifying main_activity.xml

       
    • Creating main_item.xml

       
    • Creating adapter in order to update item to Recyclerview.
    • From ui package. You need to create MainAdapter class

       
    • So take a look at R.main_item that’s xml you already created before.
    • You will come back MainActivity this class you will need to initialize adapter and using butter knife to map id from xml.

       
    • In this tutorial will be 3 functions. insert, update and delete.
    • You will click action icon button or done key. I will insert one item into Realm dabase
    • In the case you clicked specific item from recyclerview will get Inbox dialog will ask you Delete or Update. and then this is full code .

       
  6. Running app and get result.
    • Listing all item
    • Updating or deleting item.

 

Conclusion

In this tutorial you can replace SQLines or CoreDate to Realm. It’s flexible when you have big project and complicated func.

If you would like to ask me any question. Please comment below.

Hope happy coding.

Leave a Reply

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