CRUD Data (Realm) Part 2
- Get link
- X
- Other Apps
Hello sobat, saya akan memberikan artikel mengenai CRUD data pada databases menggunakan library Realms yang sebelumnya sudah kita bahas mengenai realms pada artikel sebelumnya dan sekarang masuk ke part 2 mengenai CRUD yaitu Tambah, Ubah dan Hapus data, untuk langkah-langkahnya,
Langkah PART 1
Langkah PART 1
Design Layout
Membuat layout dengan struktur
Masukan code xml pada activity_add.xml sesuai dengan struktur diatas
<?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=".activity.AddActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/inputNis" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:hint="Masukan Nis" /> <EditText android:id="@+id/inputNama" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Masukan Nama" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal"> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Hapus" android:visibility="gone" /> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Simpan" /> </LinearLayout> </LinearLayout> </RelativeLayout> |
Package Java
Masukan class dengan struktur
Masukan source code berikut di AddActivity.java sesuai dengan struktur diatas
package com.codetr.tanwir.databasesrealm.activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.codetr.tanwir.databasesrealm.R; import com.codetr.tanwir.databasesrealm.helper.RealmHelper; public class AddActivity extends AppCompatActivity { private RealmHelper realmHelper; private EditText inputNama; private EditText inputNis; private Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); setTitle("Tambah Siswa"); realmHelper = new RealmHelper(AddActivity.this); inputNis = (EditText) findViewById(R.id.inputNis); inputNama = (EditText) findViewById(R.id.inputNama); save = (Button) findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String nis, nama; nis = inputNis.getText().toString(); nama = inputNama.getText().toString(); realmHelper.addSiswa(nis, nama); finish(); Intent i = new Intent(AddActivity.this, MainActivity.class); startActivity(i); } }); } } |
Output Aplikasi
Sekian artikel dari saya semoga bermanfaat, jika ada yang ditanyakan comment saja di bawah.
- Get link
- X
- Other Apps
Comments
Post a Comment