berikut source codenya
user.java: ( membuat get and set otomatis / enkapsulasi https://komputasistat.blogspot.co.id/2017/01/cara-membuat-getter-and-setter.html )
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 { private String id; private String nama; private String alamat; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; } public String getAlamat() { return alamat; } public void setAlamat(String alamat) { this.alamat = alamat; } }
MainActivity.java
import android.content.Context; import android.content.Intent; 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; User user; @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 = realm.createObject(User.class); user.setId(id.getText().toString()); user.setNama(nama.getText().toString()); user.setAlamat(alamat.getText().toString()); realm.commitTransaction(); } }); Button btn2=(Button)findViewById(R.id.button2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent i=new Intent(MainActivity.this,LoadActivity.class); startActivity(i); } }); } }
LoadActivity.java: (Select / menampilkan data yang telah disimpan)
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import android.widget.TextView; import io.realm.Realm; import io.realm.RealmResults; /** * Created by Windows on 08/01/2017. */ public class LoadActivity extends AppCompatActivity { Realm realm; EditText id,nama,alamat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_load); TextView textView=(TextView)findViewById(R.id.textView); Realm realm = Realm.getDefaultInstance(); RealmResults<User> results1 = realm.where(User.class).findAll(); String text=""; for(User c:results1) { text=text+c.getId()+"\n"; text=text+c.getNama()+"\n"; text=text+c.getAlamat()+"\n\n\n"; } textView.setText(text); }}
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_marginTop="32dp" android:layout_below="@+id/editText3" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LOAD" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_centerHorizontal="true" /> </RelativeLayout>
activity_load.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" /> </LinearLayout>
Bila belum mengaktifkan libary realm silahkan melihat tutorial sebelumnya
https://komputasistat.blogspot.co.id/2016/10/cara-menggunakan-realm-database-1-crud.html
Hasil:
Sekian dan Terima kasih..
0 komentar:
Post a Comment