Saturday, 22 October 2016

Cara Menggunakan Realm Database 1 ( CRUD : Simple Insert ) -Android

Jika ingin mempraktekan Realm database yang lengkap dan kompleks, anda dapat mencari di google atau situs resmi https://realm.io/. Pada halaman blog ini, saya akan memberikan source code insert/ menyimpan data ke dalam database yang simple / sederhana sehingga mudah dibaca.


1. copy code di bawah ke dependencies build.grandle (Project:xxx)
classpath "io.realm:realm-gradle-plugin:2.0.2"
2. copy code di bawah ke  build.grandle (Module:app)
  apply plugin: 'realm-android'

3. Kemudian Run aplikasi (pastikan terdapat akses internet karena kita akan men-download library Realm)

4. Kita perlu membuat RealmObject untuk mendefinisikan data yang akan kita simpan.  Realm mendukung beberapa tipe data yaitu : boolean, byte, short, ìnt, long, float, double, String, Date.

source code RealmObject/ User.java:

import android.media.Image;

import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

/**
 * Created by Windows on 22/10/2016.
 */
public class User extends RealmObject {

    @PrimaryKey
    private String id;
    @Required
    private String nama;
    @Ignore
    private String alamat;

    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }
    // getters and setters
    //getters and setters
}

keterangan:
@Required - tells Realm to enforce checks that disallow null values.  
@Ignore  - supposes that a field should not be persisted to disk. 
@Index - will add a search index to the field. Due to this queries will be faster, although inserts will become slower and the data file will get larger.     
@PrimaryKey - supposes that the field is indexed.
sumber:https://dzone.com/articles/realm-practical-use-in-android
.

4. Jika ingin memulai  penympanan data pada database dimulai dengan realm.beginTransaction(); dan diakhiri dengan realm.commitTransaction();
source code MainActivity.java:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import io.realm.Realm;

public class MainActivity extends AppCompatActivity {
    Realm realm;
    EditText id,nama,alamat;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        Button btn=(Button)findViewById(R.id.button);
        id=(EditText)findViewById(R.id.editText);
        nama=(EditText)findViewById(R.id.editText2);
        alamat=(EditText)findViewById(R.id.editText3);

        Realm.init(this);
        realm=Realm.getDefaultInstance();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                realm.beginTransaction();

                User user = realm.createObject(User.class,id.getText().toString());
                user.setNama(nama.getText().toString());
                user.setAlamat(alamat.getText().toString());

                realm.commitTransaction();
            }
        });


    }
}


 5. source code 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: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="oddsaydev.realminsert.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="id"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:hint="nama"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText3"
        android:hint="alamat"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Simpan"
        android:id="@+id/button"
        android:layout_below="@+id/editText3"
        android:layout_centerHorizontal="true" />
</RelativeLayout>


Hasil:


Sekian terima kasih ..
 Nantikan source code Realm Database selanjutnya


Artikel Terkait Android ,Realm Database

1 comment:

  1. Artikel yang menarik.
    Salam kenal.
    Berkunjung ke http://bloginformatika.com ada materi kuliah & pemrograman.

    Terima kasih.

    ReplyDelete