ANDROID
TUTORIAL DATABASE
GUI DataBase
Tampilan GUI DataBase
Sintaks GUI ([Link])(1)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
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 ([Link])
(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 ([Link])
(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 ([Link])
(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 ([Link])
(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 ([Link])
(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 ([Link])
(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 ([Link])
(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 [Link];
import [Link];
import [Link];
public class DataBaseHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_NAME = "[Link]";
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
}
[Link]
Formula [Link]
package dimas.cob1;
public class Dimas_111128375Activity
extends Activity {
import [Link];
/** Called when the activity is first created.
import [Link];
*/
import [Link];
EditText nama, nim, alamat;
Import
RadioButton pil1, pil2;
[Link] Spinner spinner;
ase;
Button buat_tabel, simpan, display,
import [Link];
first, previous, next, last,
import [Link];
update,delete, exit;
import [Link];
SQLiteDatabase datavar;
import [Link];
String Nama, Alamat, Status;
import [Link]; int No, NIM;
import [Link];
TextView biodata;
import [Link];
Cursor db;
import [Link];
@Override
public void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
nama = (EditText)
findViewById([Link]);
nim = (EditText) findViewById([Link]);
alamat = (EditText)
findViewById([Link]);
pil1 = (RadioButton)
findViewById([Link].pil1);
pil2 = (RadioButton)
findViewById([Link].pil2);
buat_tabel = (Button)
findViewById([Link].buat_tabel);
simpan = (Button)
findViewById([Link]);
display = (Button)
findViewById([Link]);
first = (Button) findViewById([Link]);
previous = (Button)
findViewById([Link]);
next = (Button) findViewById([Link]);
last = (Button) findViewById([Link]);
update = (Button)
Membuat Tabel
Database
buat_tabel.setOnClickListener(new
[Link]() {
@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) {
[Link](Dimas_111128375Activity.
this,
"Database sudah ada",
Toast.LENGTH_SHORT).show();
}
}
});
Mengaktifkan fungsi Button
Simpan (1)
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
db = [Link]("select * from
sheet1;", null);
[Link]();
No = 1 + [Link]([Link](0));
Nama = [Link]().toString();
NIM =
[Link]([Link]().toString());
Alamat = [Link]().toString();
if ([Link]()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";
datavar
.execSQL("insert into
sheet1(No,Nama,NIM,Alamat,Status)
values ('"
+ No
+ "','"
+ Nama
+ "','"
+ NIM
+ "','"
+ Alamat + "','" + Status + "'); ");
[Link](Dimas_111128375Act
[Link],
"Database" + Nama + "Berhasil
Dimasukan !",
Toast.LENGTH_LONG).show();
}
Mengaktifkan fungsi Button
Simpan (2)
catch(Exception e){
db = [Link]("select * from
sheet1;", null);
[Link]();
No = 1;
Nama = [Link]().toString();
NIM =
[Link]([Link]().toString());
Alamat = [Link]().toString();
if ([Link]()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";
}
datavar
.execSQL("insert into
sheet1(No,Nama,NIM,Alamat,Status)
values ('"
+ No
+ "','"
+ Nama
+ "','"
+ NIM
+ "','"
+ Alamat + "','" + Status + "'); ");
[Link](Dimas_111128375Acti
[Link],
"Database" + Nama + "Berhasil
Dimasukan !",
Toast.LENGTH_LONG).show();
}
[Link]("");
[Link]("");
Mengaktifkan fungsi Button
Display
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
db = [Link]("select * from
sheet1;", null);
[Link]();
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
char x = [Link](4).charAt(0);
if ([Link](4).charAt(0) == 'B') {
[Link](true);
} else if ([Link](4).charAt(0)
== 'M') {
[Link](true);
}
catch (Exception e) {
// TODO: handle exception
[Link](Dimas_111128375Acti
[Link],
"Data tidak dapat ditampilkan !!!",
Toast.LENGTH_SHORT).show();
}
}
});
Mengaktifkan fungsi Button First
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//db = [Link]("select * from
sheet1;", null);
[Link]();
int y=[Link]();
[Link]([Link](y));
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
//char x = [Link](4).charAt(0);
if ([Link](4).charAt(0) == 'B') {
[Link](true);
} else if ([Link](4).charAt(0)
== 'M') {
[Link](true);
catch (Exception e) {
// TODO: handle exception
[Link](getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}
}
});
Mengaktifkan fungsi Button
Previous
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
try {
[Link]();
int y=[Link]();
[Link]([Link](y));
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
//char x = [Link](4).charAt(0);
if ([Link](4).charAt(0) == 'B')
{
[Link](true);
} else if ([Link](4).charAt(0)
== 'M') {
catch (Exception exception) {
//db = [Link]("select * from
sheet1;", null);
[Link]();
int y=[Link]();
[Link]([Link](y));
final String namapertama =
[Link](1);
[Link](getBaseContext(),
"Tidak ada sebelumnya !!!" +
namapertama + "Eror !!!",
Toast.LENGTH_LONG).show();
}
}
});
Mengaktifkan fungsi Button
Next
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//db = [Link]("select * from
sheet1;", null);
try {
[Link]();
int y=[Link]();
[Link]([Link](y));
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
if ([Link](4).charAt(0) == 'B') {
[Link](true);
} else if ([Link](4).charAt(0)
== 'M') {
[Link](true);
catch(Exception dimaserror){
[Link](getBaseContext(),
"Tidak ada data
berikutnya !!!",Toast.LENGTH_LONG).s
how();
}
}
});
Mengaktifkan fungsi Button
Last
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
[Link]();
int y=[Link]();
[Link]([Link](y));
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
if ([Link](4).charAt(0) == 'B') {
[Link](true);
} else if ([Link](4).charAt(0)
== 'M') {
[Link](true);
}}
catch (Exception e) {
// TODO: handle exception
[Link](getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}
}
});
Mengaktifkan fungsi Button
Update
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Nama = [Link]().toString();
NIM =
[Link]([Link]().toString());
Alamat = [Link]().toString();
if ([Link]()) {
Status = "Menikah";
} else {
Status = "Belum Menikah";
}
[Link]("update sheet1 set
nama='" + Nama + "',alamat='"
+ Alamat + "',status='" + Status + "'
where NIM='"
+ NIM + "';");
[Link](getBaseContext(),
"Data" + Nama + "Berhasil Update !",
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
// TODO: handle exception
[Link](getBaseContext(),
"Belum ada
data !!!",Toast.LENGTH_LONG).show();
}}
});
Mengaktifkan fungsi Button
Delete
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Nama = [Link]().toString();
NIM =
[Link]([Link]().toString());
[Link]("delete from sheet1
where nim='"
+ NIM + "';");
[Link](getBaseContext(),
"Data" + Nama + "Berhasil Dihapus !",
Toast.LENGTH_LONG).show();
[Link]("");
[Link]("");
catch (Exception e) {
// TODO: handle exception
[Link](getBaseContext(),
"Tidak ada data yang
dihapus !!!",Toast.LENGTH_LONG).show
();
}
}
});
Mengaktifkan fungsi Button
Exit
[Link](new
[Link]() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new
Intent(Intent.ACTION_MAIN);
[Link](Intent.CATEGORY_HO
ME);
[Link](Intent.FLAG_ACTIVITY_NEW
_TASK);
startActivity(intent);
}
});
}
}