Sie sind auf Seite 1von 27

ANDROID

TUTORIAL DATABASE

GUI DataBase

Tampilan GUI DataBase

Sintaks GUI (main.xml)(1)


<?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="BIODATA"
android:textStyle="bold"
android:textSize="10pt"
android:gravity="center_horizontal"
android:padding="10dp"
android:id="@+id/biodata"/>

Sintaks GUI (main.xml)


(2)
<LinearLayout
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:text="Nama
:"
android:textStyle="bold"/>
<EditText
android:text=""
android:id="@+id/nama"
android:background= "#ffffff"
android:textColor="#0000ff"

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NIM
:"
android:textStyle="bold"/>
<EditText
android:text=""
android:id="@+id/nim"
android:background= "#ffffff"
android:textColor="#0000ff"
android:layout_height="36dp"
android:layout_width="254dp"/>
</LinearLayout>

Sintaks GUI (main.xml)


(3)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alamat : "
android:textStyle="bold"
/>
<EditText
android:text=""
android:id="@+id/alamat"
android:background= "#ffffff"
android:textColor="#0000ff"
android:layout_height="36dp"
android:layout_width="254dp"/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status : "
android:textStyle="bold"
android:id="@+id/status"/>

Sintaks GUI (main.xml)


(4)

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menikah"
android:textSize="5pt"
android:id="@+id/pil1"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Belum Menikah"
android:textSize="5pt"
android:id="@+id/pil2"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>

Sintaks GUI (main.xml)


(5)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizon
tal"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:gravity="center"
android:text="BUAT TABEL"
android:textStyle="bold"
android:id="@+id/buat_tabel"
android:layout_height="wrap_content"
android:layout_width="115dp"/>

<Button
android:gravity="center"
android:text="SIMPAN"
android:textStyle="bold"
android:id="@+id/simpan"
android:layout_height="wrap_content
"
android:layout_width="97dp"/>
<Button
android:gravity="center"
android:text="DISPLAY"
android:textStyle="bold"
android:id="@+id/display"
android:layout_width="106dp"
android:layout_height="wrap_content
"/>
</LinearLayout>
</LinearLayout>

Sintaks GUI (main.xml)


(6)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal
"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="31pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="First"
android:textStyle="bold"
android:id="@+id/first" />

<Button
android:layout_width="40pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Previous"
android:textStyle="bold"
android:id="@+id/previous"/>
<Button
android:layout_width="30pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Next"
android:textStyle="bold"
android:id="@+id/next"/>
<Button
android:layout_width="35pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Last"
android:textStyle="bold"
android:id="@+id/last"/>
</LinearLayout>
</LinearLayout>

Sintaks GUI (main.xml)


(7)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizo
ntal"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="68pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Update"
android:textStyle="bold"
android:id="@+id/update" />
<Button
android:layout_width="68pt"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Delete"
android:textStyle="bold"
android:id="@+id/delete"/>
</LinearLayout>
</LinearLayout>

Sintaks GUI (main.xml)


(8)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizo
ntal"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent
"
android:gravity="center"
android:text="Exit"
android:textStyle="bold"
android:id="@+id/exit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

DataBaseHelpe
r

Formula DataBaseHelper
package dimas.cob1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_NAME = "filedb.db";
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
}

Activity.java

Formula Activity.java
package dimas.cob1;

public class Dimas_111128375Activity


extends Activity {
import android.app.Activity;
/** Called when the activity is first created.
import android.content.Intent;
*/
import android.database.Cursor;
EditText nama, nim, alamat;
Import
RadioButton pil1, pil2;
android.database.sqlite.SQLiteDatab Spinner spinner;
ase;
Button buat_tabel, simpan, display,
import android.os.Bundle;
first, previous, next, last,
import android.view.View;
update,delete, exit;
import android.widget.Button;
SQLiteDatabase datavar;
import android.widget.EditText;
String Nama, Alamat, Status;
import android.widget.RadioButton; int No, NIM;
import android.widget.Spinner;
TextView biodata;
import android.widget.TextView;
Cursor db;
import android.widget.Toast;

@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nama = (EditText)
findViewById(R.id.nama);
nim = (EditText) findViewById(R.id.nim);
alamat = (EditText)
findViewById(R.id.alamat);
pil1 = (RadioButton)
findViewById(R.id.pil1);
pil2 = (RadioButton)
findViewById(R.id.pil2);
buat_tabel = (Button)
findViewById(R.id.buat_tabel);
simpan = (Button)
findViewById(R.id.simpan);
display = (Button)
findViewById(R.id.display);
first = (Button) findViewById(R.id.first);
previous = (Button)
findViewById(R.id.previous);
next = (Button) findViewById(R.id.next);
last = (Button) findViewById(R.id.last);
update = (Button)

Membuat Tabel
Database
buat_tabel.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
datavar
.execSQL("create table sheet1(no int,
nama teks, nim int, alamat teks, status
teks);");
} catch (Exception exception) {
Toast.makeText(Dimas_111128375Activity.
this,
"Database sudah ada",
Toast.LENGTH_SHORT).show();
}
}
});

Mengaktifkan fungsi Button


Simpan (1)
simpan.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
db = datavar.rawQuery("select * from
sheet1;", null);
db.moveToLast();
No = 1 + Integer.parseInt(db.getString(0));
Nama = nama.getText().toString();
NIM =
Integer.parseInt(nim.getText().toString());
Alamat = alamat.getText().toString();
if (pil1.isChecked()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";

datavar
.execSQL("insert into
sheet1(No,Nama,NIM,Alamat,Status)
values ('"
+ No
+ "','"
+ Nama
+ "','"
+ NIM
+ "','"
+ Alamat + "','" + Status + "'); ");
Toast.makeText(Dimas_111128375Act
ivity.this,
"Database" + Nama + "Berhasil
Dimasukan !",
Toast.LENGTH_LONG).show();
}

Mengaktifkan fungsi Button


Simpan (2)
catch(Exception e){
db = datavar.rawQuery("select * from
sheet1;", null);
db.moveToLast();
No = 1;
Nama = nama.getText().toString();
NIM =
Integer.parseInt(nim.getText().toString());
Alamat = alamat.getText().toString();
if (pil1.isChecked()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";
}

datavar
.execSQL("insert into
sheet1(No,Nama,NIM,Alamat,Status)
values ('"
+ No
+ "','"
+ Nama
+ "','"
+ NIM
+ "','"
+ Alamat + "','" + Status + "'); ");
Toast.makeText(Dimas_111128375Acti
vity.this,
"Database" + Nama + "Berhasil
Dimasukan !",
Toast.LENGTH_LONG).show();
}
nama.setText("");
alamat.setText("");

Mengaktifkan fungsi Button


Display
display.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
db = datavar.rawQuery("select * from
sheet1;", null);
db.moveToFirst();
nama.setText(db.getString(1));
nim.setText(db.getString(2));
alamat.setText(db.getString(3));
char x = db.getString(4).charAt(0);
if (db.getString(4).charAt(0) == 'B') {
pil2.setChecked(true);
} else if (db.getString(4).charAt(0)
== 'M') {
pil1.setChecked(true);
}

catch (Exception e) {
// TODO: handle exception
Toast.makeText(Dimas_111128375Acti
vity.this,
"Data tidak dapat ditampilkan !!!",
Toast.LENGTH_SHORT).show();
}
}
});

Mengaktifkan fungsi Button First


first.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//db = datavar.rawQuery("select * from
sheet1;", null);
db.moveToFirst();
int y=db.getPosition();
biodata.setText(Integer.toString(y));
nama.setText(db.getString(1));
nim.setText(db.getString(2));
alamat.setText(db.getString(3));
//char x = db.getString(4).charAt(0);
if (db.getString(4).charAt(0) == 'B') {
pil2.setChecked(true);
} else if (db.getString(4).charAt(0)
== 'M') {
pil1.setChecked(true);

catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}
}
});

Mengaktifkan fungsi Button


Previous
previous.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
try {
db.moveToPrevious();
int y=db.getPosition();
biodata.setText(Integer.toString(y));
nama.setText(db.getString(1));
nim.setText(db.getString(2));
alamat.setText(db.getString(3));
//char x = db.getString(4).charAt(0);
if (db.getString(4).charAt(0) == 'B')
{
pil2.setChecked(true);
} else if (db.getString(4).charAt(0)
== 'M') {

catch (Exception exception) {


//db = datavar.rawQuery("select * from
sheet1;", null);
db.moveToFirst();
int y=db.getPosition();
biodata.setText(Integer.toString(y));
final String namapertama =
db.getString(1);
Toast.makeText(getBaseContext(),
"Tidak ada sebelumnya !!!" +
namapertama + "Eror !!!",
Toast.LENGTH_LONG).show();
}
}
});

Mengaktifkan fungsi Button


Next
next.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//db = datavar.rawQuery("select * from
sheet1;", null);
try {
db.moveToNext();
int y=db.getPosition();
biodata.setText(Integer.toString(y));
nama.setText(db.getString(1));
nim.setText(db.getString(2));
alamat.setText(db.getString(3));
if (db.getString(4).charAt(0) == 'B') {
pil2.setChecked(true);
} else if (db.getString(4).charAt(0)
== 'M') {
pil1.setChecked(true);

catch(Exception dimaserror){
Toast.makeText(getBaseContext(),
"Tidak ada data
berikutnya !!!",Toast.LENGTH_LONG).s
how();
}
}
});

Mengaktifkan fungsi Button


Last
last.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
db.moveToLast();
int y=db.getPosition();
biodata.setText(Integer.toString(y));
nama.setText(db.getString(1));
nim.setText(db.getString(2));
alamat.setText(db.getString(3));
if (db.getString(4).charAt(0) == 'B') {
pil2.setChecked(true);
} else if (db.getString(4).charAt(0)
== 'M') {
pil1.setChecked(true);
}}

catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}
}
});

Mengaktifkan fungsi Button


Update
update.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Nama = nama.getText().toString();
NIM =
Integer.parseInt(nim.getText().toString());
Alamat = alamat.getText().toString();
if (pil1.isChecked()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";
}

datavar.execSQL("update sheet1 set


nama='" + Nama + "',alamat='"
+ Alamat + "',status='" + Status + "'
where NIM='"
+ NIM + "';");
Toast.makeText(getBaseContext(),
"Data" + Nama + "Berhasil Update !",
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}}
});

Mengaktifkan fungsi Button


Delete
delete.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Nama = nama.getText().toString();
NIM =
Integer.parseInt(nim.getText().toString());
datavar.execSQL("delete from sheet1
where nim='"
+ NIM + "';");
Toast.makeText(getBaseContext(),
"Data" + Nama + "Berhasil Dihapus !",
Toast.LENGTH_LONG).show();
nama.setText("");
alamat.setText("");

catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(),
"Tidak ada data yang
dihapus !!!",Toast.LENGTH_LONG).show
();
}
}
});

Mengaktifkan fungsi Button


Exit
exit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new
Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HO
ME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW
_TASK);
startActivity(intent);
}
});
}
}

Das könnte Ihnen auch gefallen