Skip to content

Instantly share code, notes, and snippets.

@dwijonarko
Created October 21, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwijonarko/a7d8cfb3a4dc8792fd4d115d94499254 to your computer and use it in GitHub Desktop.
Save dwijonarko/a7d8cfb3a4dc8792fd4d115d94499254 to your computer and use it in GitHub Desktop.
CRUD Biodata
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/judul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/judul"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/nim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="@string/nim"
android:inputType="textPersonName" />
<EditText
android:id="@+id/nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/nama"
android:inputType="textPersonName" />
<EditText
android:id="@+id/alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/alamat"
android:inputType="textPersonName" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/email"
android:inputType="textPersonName" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:id="@+id/jenis_kelamin"
android:orientation="horizontal">
<RadioButton
android:id="@+id/jk_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text="@string/jk_l" />
<RadioButton
android:id="@+id/jk_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jk_p" />
</RadioGroup>
<Spinner
android:id="@+id/agama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:entries="@array/agama" />
<TextView
android:id="@+id/notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAlignment="center"
android:textColor="#4CAF50" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="25dp">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addBiodata"
android:text="Add" />
<Button
android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="editBiodata"
android:text="Edit" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="deleteBiodata"
android:text="Delete" />
</LinearLayout>
<ListView
android:id="@+id/biodata_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="2dp"
android:padding="5dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="agama">
<item>Islam</item>
<item>Katholik</item>
<item>Protestan</item>
<item>Hindu</item>
<item>Budha</item>
<item>Konghucu</item>
</string-array>
</resources>
package com.vokasi.projectuts;
public class Biodata {
private int id;
private String nim;
private String nama;
private String alamat;
private String email;
private String agama;
private String jenis_kelamin;
public Biodata() {
}
public Biodata(String nim, String nama, String alamat, String email, String agama, String jenis_kelamin) {
this.nim = nim;
this.nama = nama;
this.alamat = alamat;
this.email = email;
this.agama = agama;
this.jenis_kelamin = jenis_kelamin;
}
public Biodata(int id, String nim, String nama, String alamat, String email, String agama, String jenis_kelamin) {
this.id = id;
this.nim = nim;
this.nama = nama;
this.alamat = alamat;
this.email = email;
this.agama = agama;
this.jenis_kelamin = jenis_kelamin;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNim() {
return nim;
}
public void setNim(String nim) {
this.nim = nim;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAgama() {
return agama;
}
public void setAgama(String agama) {
this.agama = agama;
}
public String getJenis_kelamin() {
return jenis_kelamin;
}
public void setJenis_kelamin(String jenis_kelamin) {
this.jenis_kelamin = jenis_kelamin;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/biodata_nim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:text="NIM" />
<TextView
android:id="@+id/biodata_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/biodata_nim"
android:layout_alignParentRight="true"
android:text="NAMA" />
</RelativeLayout>
package com.vokasi.projectuts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME = "biodata.db";
private static final String TABLE_BIODATA = "biodata";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NIM = "nim";
public static final String COLUMN_NAMA = "nama";
public static final String COLUMN_ALAMAT = "alamat";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_AGAMA = "agama";
public static final String COLUMN_JENIS_KELAMIN = "jenis_kelamin";
public DBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_BIODATA_TABLE = "CREATE TABLE "+ TABLE_BIODATA + "(" + COLUMN_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT,"+ COLUMN_NIM
+ " TEXT,"+ COLUMN_NAMA +" TEXT,"+ COLUMN_ALAMAT +" TEXT,"
+ COLUMN_EMAIL +" TEXT," + COLUMN_AGAMA +" TEXT,"
+ COLUMN_JENIS_KELAMIN +" TEXT"
+")";
db.execSQL(CREATE_BIODATA_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_BIODATA);
onCreate(db);
}
public void addBiodata(Biodata biodata){
ContentValues values = new ContentValues();
values.put(COLUMN_NIM, biodata.getNim());
values.put(COLUMN_NAMA, biodata.getNama());
values.put(COLUMN_ALAMAT,biodata.getAlamat());
values.put(COLUMN_EMAIL,biodata.getEmail());
values.put(COLUMN_AGAMA,biodata.getAgama());
values.put(COLUMN_JENIS_KELAMIN,biodata.getJenis_kelamin());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_BIODATA,null,values);
db.close();
}
public void editBiodata(Biodata biodata){
ContentValues values = new ContentValues();
values.put(COLUMN_NIM, biodata.getNim());
values.put(COLUMN_NAMA, biodata.getNama());
values.put(COLUMN_ALAMAT,biodata.getAlamat());
values.put(COLUMN_EMAIL,biodata.getEmail());
values.put(COLUMN_JENIS_KELAMIN,biodata.getJenis_kelamin());
values.put(COLUMN_AGAMA,biodata.getAgama());
SQLiteDatabase db = this.getWritableDatabase();
db.update(TABLE_BIODATA,values,COLUMN_NIM+ " = ?",
new String[]{String.valueOf(biodata.getNim()) });
db.close();
}
public Biodata findBiodata(String nim){
String query = "SELECT * FROM "+ TABLE_BIODATA +
" WHERE "+COLUMN_NIM+" = \""+ nim + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
Biodata biodata = new Biodata();
if (cursor.moveToFirst()){
cursor.moveToFirst();
biodata.setId(Integer.parseInt(cursor.getString(0)));
biodata.setNim(cursor.getString(1));
biodata.setNama(cursor.getString(2));
biodata.setAlamat(cursor.getString(3));
biodata.setEmail(cursor.getString(4));
biodata.setAgama(cursor.getString(5));
biodata.setJenis_kelamin(cursor.getString(6));
cursor.close();
}else{
biodata = null;
}
db.close();
return biodata;
}
public boolean deleteBiodata (String id){
boolean result = false;
String query = "SELECT * FROM "+ TABLE_BIODATA +
" WHERE "+COLUMN_NIM+" = \""+ id + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
Biodata biodata = new Biodata();
if (cursor.moveToFirst()){
cursor.moveToFirst();
biodata.setId(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_BIODATA,COLUMN_ID+ " = ?",
new String[]{String.valueOf(biodata.getId()) });
cursor.close();
result = true;
}else{
biodata = null;
}
db.close();
return result;
}
public ArrayList<HashMap<String,String>> allBiodata(){
ArrayList<HashMap<String,String>> biodataList= new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+TABLE_BIODATA;
Cursor cursor = db.rawQuery(query,null);
while(cursor.moveToNext()){
HashMap<String,String> biodata = new HashMap<>();
biodata.put("id",cursor.getString(cursor.getColumnIndex(COLUMN_ID)));
biodata.put("nama",cursor.getString(cursor.getColumnIndex(COLUMN_NAMA)));
biodata.put("nim",cursor.getString(cursor.getColumnIndex(COLUMN_NIM)));
biodata.put("alamat",cursor.getString(cursor.getColumnIndex(COLUMN_ALAMAT)));
biodata.put("email",cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)));
biodata.put("agama",cursor.getString(cursor.getColumnIndex(COLUMN_AGAMA)));
biodata.put("jenis_kelamin",cursor.getString(cursor.getColumnIndex(COLUMN_JENIS_KELAMIN)));
biodataList.add(biodata);
}
return biodataList;
}
}
package com.vokasi.projectuts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText nim;
EditText nama;
EditText alamat;
EditText email;
RadioGroup jenis_kelamin;
RadioButton selected_jk;
Spinner agama;
TextView notification;
String[] listAgama;
ListView biodataListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nim = (EditText) findViewById(R.id.nim);
nama = (EditText) findViewById(R.id.nama);
alamat = (EditText) findViewById(R.id.alamat);
email = (EditText) findViewById(R.id.email);
jenis_kelamin = (RadioGroup) findViewById(R.id.jenis_kelamin);
agama = (Spinner) findViewById(R.id.agama);
notification=(TextView) findViewById(R.id.notification);
biodataListView = (ListView) findViewById(R.id.biodata_list);
final DBHandler handler = new DBHandler(this, null, null, 1);
final ArrayList<HashMap<String, String>> biodataList = handler.allBiodata();
String from[] = {"nim", "nama"};
int to[] = {R.id.biodata_nim, R.id.biodata_nama};
ListAdapter adapter = new SimpleAdapter(MainActivity.this, biodataList, R.layout.biodata_list, from, to);
biodataListView.setAdapter(adapter);
biodataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String,String> list = (HashMap<String,String>) biodataListView.getItemAtPosition(position);
String b_nim = String.valueOf(list.get("nim"));
Biodata biodata = handler.findBiodata(b_nim);
if (biodata != null) {
nim.setText(biodata.getNim());
nama.setText(biodata.getNama());
alamat.setText(biodata.getAlamat());
email.setText(biodata.getEmail());
if (biodata.getJenis_kelamin().equals("Laki-Laki")){
jenis_kelamin.check(R.id.jk_l);
}else{
jenis_kelamin.check(R.id.jk_p);
}
listAgama = getResources().getStringArray(R.array.agama);
switch (biodata.getAgama()) {
case "Islam":
agama.setSelection(0);
break;
case "Katholik":
agama.setSelection(1);
break;
case "Protestan":
agama.setSelection(2);
break;
case "Hindu":
agama.setSelection(3);
break;
case "Budha":
agama.setSelection(4);
break;
default:
agama.setSelection(5);
}
} else {
notification.setText("No match found");
}
}
});
}
public void addBiodata(View view) {
DBHandler dbHandler = new DBHandler(this, null, null, 1);
String b_nim = nim.getText().toString();
String b_nama = nama.getText().toString();
String b_alamat = alamat.getText().toString();
String b_email = email.getText().toString();
int jk_id = jenis_kelamin.getCheckedRadioButtonId();
selected_jk = (RadioButton) findViewById(jk_id);
String b_jk = selected_jk.getText().toString();
String b_agama = agama.getSelectedItem().toString();
Biodata biodata =new Biodata(b_nim,b_nama,b_alamat,b_email,b_agama,b_jk);
dbHandler.addBiodata(biodata);
nim.setText("");
nama.setText("");
alamat.setText("");
email.setText("");
jenis_kelamin.clearCheck();
agama.setSelection(-1);
notification.setText("Data berhasil disimpan");
final ArrayList<HashMap<String, String>> biodataList = dbHandler.allBiodata();
String from[] = {"nim", "nama"};
int to[] = {R.id.biodata_nim, R.id.biodata_nama};
ListAdapter adapter = new SimpleAdapter(MainActivity.this, biodataList, R.layout.biodata_list, from, to);
biodataListView.setAdapter(adapter);
}
public void findBiodata(View view) {
DBHandler biodataHandler = new DBHandler(this, null, null, 1);
String b_nim = nim.getText().toString();
Biodata biodata = biodataHandler.findBiodata(b_nim);
if (biodata != null) {
nim.setText(biodata.getNim());
nama.setText(biodata.getNama());
alamat.setText(biodata.getAlamat());
email.setText(biodata.getEmail());
if (biodata.getJenis_kelamin().equals("Laki-Laki")){
jenis_kelamin.check(R.id.jk_l);
}else{
jenis_kelamin.check(R.id.jk_p);
}
listAgama = getResources().getStringArray(R.array.agama);
switch (biodata.getAgama()) {
case "Islam":
agama.setSelection(0);
break;
case "Katholik":
agama.setSelection(1);
break;
case "Protestan":
agama.setSelection(2);
break;
case "Hindu":
agama.setSelection(3);
break;
case "Budha":
agama.setSelection(4);
break;
default:
agama.setSelection(5);
}
} else {
notification.setText("No match found");
}
}
public void editBiodata(View view) {
DBHandler dbHandler = new DBHandler(this, null, null, 1);
String b_nim = nim.getText().toString();
String b_nama = nama.getText().toString();
String b_alamat = alamat.getText().toString();
String b_email = email.getText().toString();
int jk_id = jenis_kelamin.getCheckedRadioButtonId();
selected_jk = (RadioButton) findViewById(jk_id);
String b_jk = selected_jk.getText().toString();
String b_agama = agama.getSelectedItem().toString();
Biodata biodata =new Biodata(b_nim,b_nama,b_alamat,b_email,b_agama,b_jk);
dbHandler.editBiodata(biodata);
nim.setText("");
nama.setText("");
alamat.setText("");
email.setText("");
jenis_kelamin.clearCheck();
agama.setSelection(-1);
notification.setText("Data berhasil disimpan");
final ArrayList<HashMap<String, String>> biodataList = dbHandler.allBiodata();
String from[] = {"nim", "nama"};
int to[] = {R.id.biodata_nim, R.id.biodata_nama};
ListAdapter adapter = new SimpleAdapter(MainActivity.this, biodataList, R.layout.biodata_list, from, to);
biodataListView.setAdapter(adapter);
}
public void deleteBiodata(View view) {
DBHandler dbHandler = new DBHandler(this, null, null, 1);
String b_nim = nim.getText().toString();
boolean result = dbHandler.deleteBiodata(b_nim);
if (result) {
notification.setText("Record deleted");
nim.setText("");
nama.setText("");
alamat.setText("");
email.setText("");
jenis_kelamin.clearCheck();
agama.setSelection(-1);
notification.setText("Data berhasil dihapus");
} else {
notification.setText("No match found");
}
final ArrayList<HashMap<String, String>> biodataList = dbHandler.allBiodata();
String from[] = {"nim", "nama"};
int to[] = {R.id.biodata_nim, R.id.biodata_nama};
ListAdapter adapter = new SimpleAdapter(MainActivity.this, biodataList, R.layout.biodata_list, from, to);
biodataListView.setAdapter(adapter);
}
}
<resources>
<string name="app_name">Project UTS</string>
<string name="judul">Biodata Mahasiswa</string>
<string name="nim">Nomor Induk Mahasiswa</string>
<string name="nama">Nama Mahasiswa</string>
<string name="alamat">Alamat</string>
<string name="email">E-mail</string>
<string name="jk_l">Laki-Laki</string>
<string name="jk_p">Perempuan</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment