Anda di halaman 1dari 12

LAPORAN PEMOGRAMAN MOBIE

“PROGRAM DATABASE SQLLITE ANDROID-ECLIPSE”

Disusun oleh :
1. IKA PURDIASARI 888740305190018
2. HENGKY SUDARSONO 888740305190121
3. IMANIAR WURYANDARI 3322186405010003
4. FEBLATIAN BAHAGIA DINI RAHAYU 888740305190026
Mata Kuliah :
PEMOGRAMAN MOBILE
DOSEN :
PRIYADI, M.KOM

UNNIVERSITAS STEKOM UNGARAN


2021
1. Buat file baru dengan nama “Tugas Uas db Sqilite” atau menyesuaikan kebutuhan anda
Dengan cara klik file>new>Android Application Project, ikuti step selanjutnya dengan klik next

2. Langkah selanjutnya masuk kedalam tampilan values>strings.xml


3. Masuk ke Navigator klik res/layout/activity_main.xml

4. Langkah selanjutnya ketikkan kode berikut kemudian save :


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Masukkan nama dan alamat</string>
<string name="app_name">Database SQLLite Android hengky-
ima</string>
<string name="btnAddtxt">Add</string>
<string name="namaLabel">Nama</string>
<string name="alamatLabel">Alamat</string>
<string name="nomorLabel">No.</string>
<string name="btnGetRow">Get</string>
<string name="btnUpdateRow">Update</string>
<string name="ketUpdate">Sebelum mengubah data,
pilih dulu baris ke berapa
data yang akan diubah</string>
<string name="ketAmbilBaris">Update data pada baris
</string>
<string name="ketDelete">Tulis no. baris
yang akan di delete,
lalu klik tombol &quot;delete&quot;</string>
<string name="btnDel">Delete</string>
</resources>
5. Langkah berikutnya lakukan modifikasi tampilan melalui res/layout/activity_main_activity.xml,
isikan dengan coding berikut:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<EditText android:id="@+id/inNama"
android:layout_height="wrap_content"
android:layout_width="100dip"></EditText>
<EditText android:id="@+id/inHobi"
android:layout_height="wrap_content"
android:layout_width="100dip"></EditText>
<Button android:layout_width="wrap_content"
android:id="@+id/btnAdd"
android:layout_height="wrap_content"
android:text="@string/btnAddtxt"></Button>
</LinearLayout>
<View android:layout_width="wrap_content"
android:id="@+id/view2"
android:layout_height="1dip"
android:background="#FF909090"></View>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="@string/ketUpdate"></TextView>
<View android:layout_width="wrap_content"
android:id="@+id/view2"
android:layout_height="1dip"
android:background="#FF909090"></View>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout2"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:text="@string/ketAmbilBaris"
android:layout_height="wrap_content"
android:id="@+id/textView2"></TextView>
<EditText android:layout_height="wrap_content"
android:id="@+id/inGetId"
android:layout_width="50dip"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnGetRow"
android:id="@+id/btnGetId"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout3"
android:layout_height="wrap_content">
<EditText android:layout_height="wrap_content"
android:layout_width="100dip"
android:id="@+id/inUpdateNama"></EditText>
<EditText android:layout_height="wrap_content"
android:layout_width="100dip"
android:id="@+id/inUpdateAlamat"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnUpdateRow"
android:id="@+id/btnUpdate"></Button>
</LinearLayout>
<View android:layout_width="wrap_content"
android:id="@+id/view2"
android:layout_height="1dip"
android:background="#FF909090"></View>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView3"
android:text="@string/ketDelete"></TextView>
<LinearLayout android:id="@+id/linearLayout4"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<EditText android:layout_height="wrap_content"
android:layout_width="50dip"
android:id="@+id/idDelete"></EditText>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnDel"
android:text="@string/btnDel"></Button>
</LinearLayout>
<View android:layout_width="wrap_content"
android:id="@+id/view2"
android:layout_height="1dip"
android:background="#FF909090"></View>
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/tabel_data">
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="50dip"
android:text="@string/nomorLabel"
android:id="@+id/no_id"></TextView>
<TextView android:layout_height="wrap_content"
android:layout_width="100dip"
android:text="@string/namaLabel"
android:id="@+id/nama_id"></TextView>
<TextView android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/alamatLabel"
android:id="@+id/alamat_id"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
6. Karena pada project ini tidak menggunakan menu, maka kode menu dihilangkan kemudian save
7. Buat Package baru dengan nama DatabaseManager.java
Pilih file>new>class setelah muncul jendela java class beri nama dan klik finish

8. Setelah itu, akan muncul tampilan :

Kemudian ketikan koding berikut pada baris kedua kemudian save :

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DatabaseManager {
private static final String ROW_ID = "_id";
private static final String ROW_NAMA = "nama";
private static final String ROW_HOBI = "alamat";
private static final String NAMA_DB = "DatabaseAndroidDua";
private static final String NAMA_TABEL = "datamemberbarumbs";
private static final int DB_VERSION = 1;
private static final String CREATE_TABLE = "create table " +
NAMA_TABEL
+ " (" + ROW_ID + " integer PRIMARY KEY autoincrement," +
ROW_NAMA
+ " text," + ROW_HOBI + " text)";

private final Context context;


private DatabaseOpenHelper dbHelper;
private SQLiteDatabase db;
public DatabaseManager(Context ctx) {
this.context = ctx;
dbHelper = new DatabaseOpenHelper(ctx);
db = dbHelper.getWritableDatabase();
}
public static class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context context) {
super(context, NAMA_DB, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
db.execSQL("DROP TABLE IF EXISTS " + NAMA_DB);
onCreate(db);
}
}
public void close() {
dbHelper.close();
}

public void addRow(String nama, String alamat) {

ContentValues values = new ContentValues();


values.put(ROW_NAMA, nama);
values.put(ROW_HOBI, alamat);

try {
db.insert(NAMA_TABEL, null, values);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}

public ArrayList<ArrayList<Object>> ambilSemuaBaris() {


ArrayList<ArrayList<Object>> dataArray = new
ArrayList<ArrayList<Object>>();
Cursor cur;
try {
cur = db.query(NAMA_TABEL, new String[] { ROW_ID, ROW_NAMA,
ROW_HOBI }, null, null, null, null, null);
cur.moveToFirst();
if (!cur.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cur.getLong(0));
dataList.add(cur.getString(1));
dataList.add(cur.getString(2));
dataArray.add(dataList);
} while (cur.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
Log.e("DEBE ERROR", e.toString());
Toast.makeText(context, "gagal ambil semua baris:" +
e.toString(),
Toast.LENGTH_SHORT).show();
}
return dataArray;
}

public ArrayList<Object> ambilBaris(long rowId) {

ArrayList<Object> arrbaris = new ArrayList<Object>();


Cursor cursor;
try {
cursor = db.query(NAMA_TABEL, new String[] { ROW_ID, ROW_NAMA,
ROW_HOBI }, ROW_ID + "=" + rowId, null, null, null,
null,
null);
cursor.moveToFirst();

if (!cursor.isAfterLast()) {
do {
arrbaris.add(cursor.getLong(0));
arrbaris.add(cursor.getString(1));
arrbaris.add(cursor.getString(2));
} while (cursor.moveToNext());
String r = String.valueOf(arrbaris);
Toast.makeText(context, "haha" + r,
Toast.LENGTH_SHORT).show();
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("error", e.toString());
Toast.makeText(context, "hhii" + e.toString(),
Toast.LENGTH_SHORT)
.show();
}
return arrbaris;
}

public void updateBaris(long rowId, String nama, String alamat) {

ContentValues cv = new ContentValues();


cv.put(ROW_NAMA, nama);
cv.put(ROW_HOBI, alamat);
try {
db.update(NAMA_TABEL, cv, ROW_ID + "=" + rowId, null);
} catch (Exception e) {
e.printStackTrace();
Log.e("Db Error", e.toString());
}
}

public void deleteBaris(long idBaris) {


try {
db.delete(NAMA_TABEL, ROW_ID + "=" + idBaris, null);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", e.toString());
}
}

}
9. Kemudian masuk ke main activity.java dan isikan koding berikut mulai baris kedua dan
kemudian save
import java.util.ArrayList;
import com.example.hengkyimaproject.DatabaseManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


DatabaseManager dm;
EditText nama, hobi, GetId, updateNama, updateAlamat, idDel;
Button addBtn, getIdBtn, updateBtn, delBtn;
TableLayout tabel4data;// tabel for data

/** Called when the activity is first created. */


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
dm = new DatabaseManager(this);
setupView();
fungsiBtn();
updateTable();
}
public void setupView() {
tabel4data = (TableLayout) findViewById(R.id.tabel_data);
nama = (EditText) findViewById(R.id.inNama);
hobi = (EditText) findViewById(R.id.inHobi);
updateNama = (EditText) findViewById(R.id.inUpdateNama);
updateAlamat = (EditText) findViewById(R.id.inUpdateAlamat);
GetId = (EditText) findViewById(R.id.inGetId);
idDel = (EditText) findViewById(R.id.idDelete);

addBtn = (Button) findViewById(R.id.btnAdd);


getIdBtn = (Button) findViewById(R.id.btnGetId);
updateBtn = (Button) findViewById(R.id.btnUpdate);
delBtn = (Button) findViewById(R.id.btnDel);
}

public void fungsiBtn() {


addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simpKamuta();
kosongkanField();
}
});
getIdBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View b) {
ambilBaris();
}
});
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View c) {
updateBaris();
kosongkanField();
}
});
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View d) {
// TODO Auto-generated method stub
deleteData();
kosongkanField();
}
});
}
// . fungsi button

protected void kosongkanField() {


nama.setText("");
hobi.setText("");
updateNama.setText("");
updateAlamat.setText("");
GetId.setText("");
idDel.setText("");
}

private void deleteData() {


dm.deleteBaris(Long.parseLong(idDel.getText().toString()));
updateTable();
}
protected void updateBaris() {
dm.updateBaris(Long.parseLong(GetId.getText().toString()), updateNama
.getText().toString(), updateAlamat.getText().toString());
updateTable();
}

private void ambilBaris() {


try {
ArrayList<Object> baris;
baris =
dm.ambilBaris(Long.parseLong(GetId.getText().toString()));
updateNama.setText((String) baris.get(1));
updateAlamat.setText((String) baris.get(2));
} catch (NumberFormatException e) {
e.printStackTrace();
Log.e("eror db", e.toString());
Toast.makeText(getBaseContext(),
e.toString(),Toast.LENGTH_LONG).show();
}}

// penyimpanan data
protected void simpKamuta() {
try {
dm.addRow(nama.getText().toString(),
hobi.getText().toString());
updateTable();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"gagal simpan,"+
e.toString(),Toast.LENGTH_LONG).show();
}
}
// . penyimpanan data

// update tabel
protected void updateTable() {
while (tabel4data.getChildCount() > 1) {
tabel4data.removeViewAt(1);
}
ArrayList<ArrayList<Object>> data = dm.ambilSemuaBaris();//
for (int posisi = 0; posisi < data.size(); posisi++) {
TableRow tabelBaris = new TableRow(this);
ArrayList<Object> baris = data.get(posisi);

TextView idTxt = new TextView(this);


idTxt.setText(baris.get(0).toString());
tabelBaris.addView(idTxt);

TextView namaTxt = new TextView(this);


namaTxt.setText(baris.get(1).toString());
tabelBaris.addView(namaTxt);

TextView hobiTxt = new TextView(this);


hobiTxt.setText(baris.get(2).toString());
tabelBaris.addView(hobiTxt);

tabel4data.addView(tabelBaris);
}
}
// . update tabel

10. Selanjutnya jalankan program, dan jika program berhasil akan muncul tampilan :

Video hasil bisa dicek di : https://youtu.be/x2mJOv7FnVs

Anda mungkin juga menyukai