You are on page 1of 50

MODUL PRAKTIKUM

MOBILE PROGRAMMING

Disusun oleh :
Andi Chairunnas, M.Kom., M.Pd.

LABORATORIUM WORKSHOP
PROGRAM STUDI ILMU KOMPUTER
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS PAKUAN
BOGOR
2019
KATA PENGANTAR
Puji syukur penyusun panjatkan kehadirat Tuhan YME yang telah memberikan
petunjuk dan hidayah-Nya sehingga penyusun dapat menyelesaikan modul praktikum
Mobile Programming ini.
Modul praktikum ini disusun sebagai bahan praktikum mata kuliah Mobile
Programming yang di ambil oleh mahasiswa Program Studi Ilmu Komputer
Universitas Pakuan pada semester Genap.
Kehadiran modul praktikum Mobile Programming ini diharapkan akan
membantu atau memudahkan mahasiswa dalam mengaplikasikan teori yang telah
diperoleh selama proses kuliah di kelas, disamping itu juga diharapkan dapat
menambah wawasan mahasiswa mengenai Mobile Programming itu sendiri dan
aplikasinya di kehidupan sehari-hari.
Penyusun menyadari bahwa dalam penyajian materi yang terdapat dalam modul
praktikum Mobile Programming ini masih belum dapat mewakili seluruh materi dalam
Mobile Programmong itu sendiri. Kritik dan saran sangat terbuka untuk kesempurnaan
modul praktikum Mobile Programming ini dimasa mendatang.
Akhir kata, semoga kehadiran modul praktikum Mobile Programming ini dapat
bermanfaat bagi kita semua, Amin.

Bogor, Februari 2019

Penyusun

ii
TATA TERTIB PRAKTIKUM

1. Peserta praktikum hadir 10 menit sebelum praktikum dimulai.

2. Wajib memakai pakaian rapih dan sepatu tertutup.

3. Membawa kartu praktikum.

4. Dilarang makan dan minum selama praktikum berlangsung.

5. Dilarang meninggalkan laboratorium selama praktkum berlangsung, kecuali atas


ijin assisten atau dosen yang bersangkutan.

iii
DAFTAR ISI
KATA PENGANTAR .....................................................................…ii
TATA TERTIB PRAKTIKUM .......................................................…iii
DAFTAR ISI ....................................................................................…iv
BAB I : LAYOUT DAN JAVA(Menu Login) ....................….1
BAB II : FORM DATA MAHASISWA…………....................8
BAB III : FORM KALKULASI NILAI.....................................22
BAB IV : SESSION PADA ANDROID…….…………….…..26
BAB V : INSERT, READ,UPDATE,DELETE (CRUD) ........32

DAFTAR PUSTAKA .........................................................................46

iv
BAB I
LAYOUT DAN JAVA(Menu Login)

Membuat layout atau tampilan interface di Android seperti yang


kita inginkan atau tampilan kepada layar seperti tampilan Text View, Edit
Text, Radio Button, Spinner, dan Button sehingga dapat difungsikan
sebagaimana kegunaan dari masing-masing widget nya perlu membuat
.xml dari aplikasi yang akan kita buat dengan cara :
Buatlah Project android dengan ketentuan sebagai berikut :
Application Name : Pertemuan1
Company Domain : pertemuan1.example.com
Project Location : (Sesuai Keinginan)
Package Name : com.example.p1
Min SDK : API 16 (Jelly Bean)
Target SDK : API 17 (Android 4.2)
 Klik menu File pada Android Studio – New – New Project

 Isikan Project dengan ketentuan sama seperti yang sudah dijelaskan


di atas.
 Pilih Empty Activity Kemudian Pilih Next

 Biarkan Activity Name dan Layout Name nya seperti gambar


dibawah. Kemudian pilih Finish (Untuk Activity Name dan Layout
Name hanya penamaan untuk di File java dan File xml jadi bisa diisi
bebas.)

2
 Setelah itu masuk ke halaman codingan yang terbagi dengan
beberapa tugasnya, diantaranya untuk codingan java, xml, string,
background dll ada sebelah kiri :

 Lalu Ketikan Program berikut di dalam Project App – Res –


Layout – activity_main.xml
Main.xml
<?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:layout_margin="20dp"
tools:context=".MainActivity">

3
<ImageView
android:id="@+id/gmbr"
android:layout_width="match_parent"
android:layout_height="200sp"
android:textSize="50sp"
android:textAlignment="center"
android:paddingTop="50sp"
android:src="@drawable/ic_face" />

<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/masukan_username"
android:textSize="20sp"
android:textColor="#000"/>

<EditText
android:id="@+id/username2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />

<TextView
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Password :"
android:textSize="20sp"
android:textColor="#000"/>

<EditText
android:id="@+id/password2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>

<CheckBox
android:id="@+id/boxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="show password"/>

<Button
android:id="@+id/button"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="15sp"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_face"/>
android:drawableLeft="@drawable/ic_face"/>

4
<Button
android:id="@+id/button2"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Register"
android:textSize="15sp"
android:drawableLeft="@drawable/ic_face"/>

<TextView
android:id="@+id/tampil"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

 Karna pertemuan pertama ini kita menggunakan dua tampilan, maka


buat codingan baru pada layout dengan cara yang sama seperti
diatas di dalam project Java-com.exmple.asus.pertemuan1-
MainActivity
MainActivity.Java
package com.example.yanmed.pertemuan1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener, CompoundButton.OnCheckedChangeListener {
ImageView gambar;
EditText username2, password2;
Button btn1, btn2;
TextView tampil;
CheckBox unhide;
String username = "admin";
String password = "admin";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gambar = (ImageView) findViewById(R.id.gmbr);
username2 = (EditText) findViewById(R.id.username2);

5
password2 = (EditText) findViewById(R.id.password2);
btn1 = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
tampil = (TextView) findViewById(R.id.tampil);
unhide = (CheckBox) findViewById(R.id.boxx);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
unhide.setOnCheckedChangeListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String user = username2.getText().toString();
String pass = password2.getText().toString();
if (username.equals(user)&&password.equals(pass)) {
Toast.makeText(this, "Login",
Toast.LENGTH_SHORT).show();
Intent pindah = new Intent(this,
Main2Activity.class);
startActivity(pindah);
finish();
}
else {
Toast.makeText(this, "Gagal",
Toast.LENGTH_SHORT).show();
}

break;
case R.id.button2:
Toast.makeText(this, "Register",
Toast.LENGTH_SHORT).show();
break;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (!isChecked) {

password2.setTransformationMethod(PasswordTransformationMethod.getIn
stance());
} else {

password2.setTransformationMethod(HideReturnsTransformationMethod.ge
tInstance());
}
}
}

6
 Jika untuk melihat tampilannya pada graphical layout maka akan
seperti dibawah ini.

 Untuk melihat tampilan yang kedua sama yaitu pada graphical


layout seperti dibawah ini.

 Setelah di debug maka hasil dari Layout yang telah dibuat akan
tampil seperti gambar berikut ini :

7
BAB II
FORM DATA MAHASISWA

Membuat layout atau tampilan interface di Android seperti yang


kita inginkan atau tampilan kepada layar seperti tampilan Text View, Edit
Text, Radio Button, Spinner, dan Button sehingga dapat difungsikan
sebagaimana kegunaan dari masing-masing widget nya perlu membuat
.xml dari aplikasi yang akan kita buat dengan cara :
 Klik menu File pada Android Studio – New – New Project

 Isikan Project dengan ketentuan sama seperti yang sudah dijelaskan


di atas.

8
 Pilih Empty Activity Kemudian Pilih Next

 Biarkan Activity Name dan Layout Name nya seperti gambar


dibawah. Kemudian pilih Finish (Untuk Activity Name dan Layout
Name hanya penamaan untuk di File java dan File xml jadi bisa diisi
bebas.)

9
 Setelah itu masuk ke halaman codingan yang terbagi dengan
beberapa tugasnya, diantaranya untuk codingan java, xml, string,
background dll ada sebelah kiri :

 Lalu Ketikan Program berikut di dalam Project App – Res –


Layout – activity_main.xml

10
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yanmed.pertemuan2.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#e77eee"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="6">

<ImageView
android:id="@+id/pakuan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_account_balance_black_24dp"
android:layout_weight="70" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="30">

<TextView
android:id="@+id/unpak"
android:textColor="#fcfcfb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="WORKSHOP"
android:paddingTop="15dp"/>
<TextView
android:id="@+id/mipa"
android:textColor="#fcfcfb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="UNIVERSITAS PAKUAN"/>
</LinearLayout>
</LinearLayout>

11
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fcfcfb"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:layout_weight="1">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/npm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NPM"/>

<EditText
android:id="@+id/innpm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:inputType="number"
android:hint="masukkan no induk mahasiswa"/>

<TextView
android:id="@+id/nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama"/>
<EditText
android:id="@+id/innama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="masukkan nama mahasiswa"/>

<TextView
android:id="@+id/jk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jenis Kelamin"/>

12
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/cowo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Laki-laki"/>
<RadioButton
android:id="@+id/cewe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Perempuan"/>
</RadioGroup>
<TextView
android:id="@+id/kelas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kelas"/>

<Spinner
android:id="@+id/skelas"
android:entries="@array/pilihankelas"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>

<TextView
android:id="@+id/agama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agama"/>

<Spinner
android:id="@+id/sagama"
android:entries="@array/pilihanagama"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
<TextView
android:id="@+id/tempat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tempat Lahir"/>

<EditText
android:id="@+id/intempat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="masukkan tempat lahir"/>

<TextView
android:id="@+id/tgl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal Lahir"/>

13
<EditText
android:id="@+id/intgl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:hint="dd/mm/yy"/>
<Button
android:id="@+id/tombol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simpan"/>

<TextView
android:id="@+id/hasil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>

 Jika masih ada yang error itu tidak apa-apa dikarenakan pada widget spinner
tersebut resource @array/pilihankelas dan @array/pilihanagama belum terisi
data

14
 Buka file string.xml yang ada di dalam folder project P2 – res – values-string
String.xml

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


<resources>
<string name="app_name">P1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>

<string-array name="pilihankelas">
<item >--Pilih--</item>
<item >A</item>
<item >B</item>
<item >C</item>
<item >D</item>
<item >E</item>
<item >F</item>
<item >G</item>
<item >H</item>
<item >I</item>
<item >J</item>
<item >K</item>
<item >L</item>
<item >M</item>
<item >N</item>
</string-array>

<string-array name="pilihanagama">
<item >--Pilih--</item>
<item >Islam</item>
<item >Katholik</item>
<item >Protestan</item>
<item >Budha</item>
<item >Hindu</item>
<item >Konghucu</item>
</string-array>
</resources>

15
Buat lagi layout baru res-Klik Kanan pada layout-New-XML-Layout XML File

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="5dp"
android:text=""
android:textSize="15dp"
android:typeface="serif"/>
<TextView
android:id="@+id/npm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text=""
android:textSize="15dp"
android:typeface="serif"/>
<TextView
android:id="@+id/kelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text=""
android:textSize="15dp"
android:typeface="serif"/>

16
<TextView
android:id="@+id/agamaa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="5dp"
android:text=""
android:textSize="15dp"
android:typeface="serif"/>
<TextView
android:id="@+id/jkk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text=""
android:textSize="15dp"
android:typeface="serif"/>

17
 Isikan kode program kedalam MainActvity.java
MainActivity.java
package com.example.yanmed.pertemuan2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


ImageView foto;
TextView tnpm, tnama, ttl, ttgl, tjk, tkls, tagm, thasil, kt;
EditText enpm, enama, etempat, etgl;
RadioGroup rg1;
RadioButton jk;
Button tmbl;
Spinner s1, s2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foto=(ImageView) findViewById(R.id.pakuan);
tnpm=(TextView)findViewById(R.id.npm);
tnama=(TextView)findViewById(R.id.nama);

tjk=(TextView)findViewById(R.id.jk);
tkls=(TextView)findViewById(R.id.kelas);
tagm=(TextView)findViewById(R.id.agama);
ttl=(TextView)findViewById(R.id.tempat);
ttgl=(TextView)findViewById(R.id.tgl);
thasil=(TextView)findViewById(R.id.hasil);
enpm=(EditText)findViewById(R.id.innpm);
enama=(EditText)findViewById(R.id.innama);
etempat=(EditText)findViewById(R.id.intempat);
etgl=(EditText)findViewById(R.id.intgl);
rg1=(RadioGroup)findViewById(R.id.rg);
tmbl=(Button)findViewById(R.id.tombol);
s1=(Spinner)findViewById(R.id.skelas);
s2=(Spinner)findViewById(R.id.sagama);

18
tmbl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String inputnpm =
String.valueOf(enpm.getText().toString());
String inputnama =
String.valueOf(enama.getText().toString());
String inputtempat =
String.valueOf(etempat.getText().toString());
String inputtanggal =
String.valueOf(etgl.getText().toString());
String inputkelas =
String.valueOf(s1.getSelectedItem().toString());
String inputagama =
String.valueOf(s2.getSelectedItem().toString());

int pilihidjk = rg1.getCheckedRadioButtonId();


jk = (RadioButton) findViewById(pilihidjk);

Intent intent = new Intent(MainActivity.this,


Layout.class);
Bundle b = new Bundle();
b.putString("parse_npm", inputnpm);
b.putString("parse_nama", inputnama);
b.putString("parse_kls", inputkelas);
b.putString("parse_agm", inputagama);
b.putString("parse_jk", jk.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
Buat Java });
baru untuk Layout ke 2
}
}

19
Buat java baru untuk layout ke 2
Buka file Java-Klik kanan com.example.asus-New-Java Class
package com.example.yanmed.pertemuan2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Layout extends AppCompatActivity {

TextView nama,kelas,npm,agama,jk
String getnpm, getnama, getkelas, getjk,getagama;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);

npm = (TextView)findViewById(R.id.npm);
nama = (TextView)findViewById(R.id.nama);
kelas = (TextView)findViewById(R.id.kelas);
agama= (TextView)findViewById(R.id.agamaa);
jk = (TextView)findViewById(R.id.jkk);

Bundle b = getIntent().getExtras();
getnpm = b.getString("parse_npm");
getnama = b.getString("parse_nama");
getkelas = b.getString("parse_kls");
getjk = b.getString("parse_jk");
getagama = b.getString ("parse_agm");

npm.setText("NPM : "+getnpm);
nama.setText("NAMA : "+getnama);
kelas.setText("KELAS : "+getkelas);
jk.setText("JENIS KELAMIN : "+getjk);
agama.setText("AGAMA : "+getagama);

20
 Debug program, kemudian Run Application
 Tampilan saat launch program

21
BAB III
Kalkulasi Nilai

Buat Proyek Baru dengan nama Kalkulasi Nilai dengan EMPTY ACTIVITY

activity_main

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.yanmed.myapplication.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a7d1d1">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Form Nilai Mahasiswa"
android:textColor="#000"
android:textSize="25dp"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai Kehadiran"
android:textStyle="bold" />

22
<EditText
android:id="@+id/enilaiPrak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai Tugas"
android:textStyle="bold" />

<EditText
android:id="@+id/enilaiTgs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai UAS"
android:textStyle="bold" />

<EditText
android:id="@+id/enilaiUAS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<Button
android:id="@+id/btnHitung"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hitung" />
<Button
android:id="@+id/hapus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hapus" />

23
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hasil Nilai Akhir " />

<TextView
android:id="@+id/angka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/huruf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>

</LinearLayout>

MainActivity.Java

package com.example.yanmed.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
TextView angk, hrf;
EditText absen, uts, prak, tugas, uas;
Button hitung, hps, save;
String Save_huruf, Save_Nilai, ABSEN, Huruf, Nilai;
String Nilai_huruf = null;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
angk = (TextView) findViewById(R.id.angka);
hrf = (TextView) findViewById(R.id.huruf);
absen = (EditText) findViewById(R.id.enilaiabsen);
uts = (EditText) findViewById(R.id.enilaiUTS);
prak = (EditText) findViewById(R.id.enilaiPrak);
tugas = (EditText) findViewById(R.id.enilaiTgs);
uas = (EditText) findViewById(R.id.enilaiUAS);
hitung = (Button) findViewById(R.id.btnHitung);
hps = (Button) findViewById(R.id.hapus);

24
hitung.setOnClickListener(this);
hps.setOnClickListener(this);
save.setOnClickListener(this);

SharedPreferences sharedpreferences = getSharedPreferences("DATA",


Context.MODE_PRIVATE);
Nilai = sharedpreferences.getString("Nilai", "");
Huruf = sharedpreferences.getString("Huruf", "");
ABSEN = sharedpreferences.getString("absen", "");

if (!Nilai.isEmpty() || !Huruf.isEmpty()) {
angk.setText(Nilai);
hrf.setText(Huruf);
absen.setText(ABSEN);
}

 Debug program, kemudian Run Application


 Tampilan pertama saat launch program

25
BAB IV
SESSION PADA ANDROID
Session ini digunakan untuk menyimpan informasi, yang mana suatu waktu akan
digunakan kembali oleh aplikasi.
Buat proyek baru seperti di Bab 1 dengan nama pertemuan3

activity_main

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.yanmed.myapplication.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a7d1d1">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Form Nilai Mahasiswa"
android:textColor="#000"
android:textSize="25dp"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai Kehadiran"
android:textStyle="bold" />

26
<EditText
android:id="@+id/enilaiabsen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai UTS"
android:textStyle="bold" />

<EditText
android:id="@+id/enilaiUTS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai Praktikum"
android:textStyle="bold" />

<EditText
android:id="@+id/enilaiPrak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai Tugas"
android:textStyle="bold" />

<EditText
android:id="@+id/enilaiTgs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Masukan Nilai UAS"
android:textStyle="bold" />

27
<EditText
android:id="@+id/enilaiUAS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:maxLength="3"/>

<Button
android:id="@+id/btnHitung"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hitung" />
<Button
android:id="@+id/simpen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simpan Nilai" />
<Button
android:id="@+id/hapus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hapus" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hasil Nilai Akhir " />

<TextView
android:id="@+id/angka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/huruf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>

</LinearLayout>

MainActivity.Java

package com.example.yanmed.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

28
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
angk = (TextView) findViewById(R.id.angka);
hrf = (TextView) findViewById(R.id.huruf);
absen = (EditText) findViewById(R.id.enilaiabsen);
uts = (EditText) findViewById(R.id.enilaiUTS);
prak = (EditText) findViewById(R.id.enilaiPrak);
tugas = (EditText) findViewById(R.id.enilaiTgs);
uas = (EditText) findViewById(R.id.enilaiUAS);
hitung = (Button) findViewById(R.id.btnHitung);
hps = (Button) findViewById(R.id.hapus);
save = (Button) findViewById(R.id.simpen);

hitung.setOnClickListener(this);
hps.setOnClickListener(this);
save.setOnClickListener(this);

SharedPreferences sharedpreferences = getSharedPreferences("DATA",


Context.MODE_PRIVATE);
Nilai = sharedpreferences.getString("Nilai", "");
Huruf = sharedpreferences.getString("Huruf", "");
ABSEN = sharedpreferences.getString("absen", "");

if (!Nilai.isEmpty() || !Huruf.isEmpty()) {
angk.setText(Nilai);
hrf.setText(Huruf);
absen.setText(ABSEN);
}

29
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnHitung:

double a = Double.parseDouble(absen.getText().toString());
double b = Double.parseDouble(uts.getText().toString());
double c = Double.parseDouble(prak.getText().toString());
double d = Double.parseDouble(tugas.getText().toString());
double e = Double.parseDouble(uas.getText().toString());

double nilai = (a * 0.1) + (b * 0.2) + (c * 0.25) + (d *


0.15) + (d * 0.15) + (e * 0.3);

Save_Nilai = String.valueOf(nilai);

angk.setText("Angka : " + Save_Nilai);


if (nilai >= 85) {
Nilai_huruf = "Huruf : A ";
} else if ((nilai < 84) && (nilai >= 70)) {
Nilai_huruf = "Huruf : B ";
} else if ((nilai < 70) && (nilai >= 55)) {
Nilai_huruf = "Huruf : C ";
} else if ((nilai < 55) && (nilai >= 40)) {
Nilai_huruf = "Huruf : D ";
} else if ((nilai < 40) && (nilai >= 00)) {
Nilai_huruf = "Huruf : E ";
}
hrf.setText(Nilai_huruf);
Save_huruf = Nilai_huruf;
break;
case R.id.simpen:
sharedpreferences = getSharedPreferences("DATA",
Context.MODE_PRIVATE);
editor = sharedpreferences.edit();
editor.putString("absen", absen.getText().toString());
editor.commit();
editor.putString("Nilai", "Angka : " + Save_Nilai);
editor.commit();

break;
case R.id.hapus:
absen.setText("");
uts.setText("");
prak.setText("");
tugas.setText("");
uas.setText("");
angk.setText("");
hrf.setText("");
sharedpreferences = getSharedPreferences("DATA",
Context.MODE_PRIVATE);
editor = sharedpreferences.edit();
if (!Nilai.isEmpty() || !Huruf.isEmpty()) {
editor.clear();
editor.apply();
}
break; } }}

30
 Debug program, kemudian Run Application
 Tampilan pertama saat launch program

31
BAB V
INSERT, READ,UPDATE,DELETE (CRUD)

Buat proyek baru seperti di Bab 1 dengan nama CRUD, dengan BASIC ACTIVITY

Basic Activity
activity_main.xml
Tampilan untuk container floating action button.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.example.yanmed.pertemuan5.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

32
<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml
Sebagai tampilan dari data-data pada listview.

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


<RelativeLayout
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:id="@+id/content_main"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.yanmed.pertemuan5.MainActivity"
tools:showIn="@layout/activity_main">

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

33
list_row.xml
Sebagai tampilan custom listview yang berisi nama dan alamat.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

<TextView
android:id="@+id/name"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/address"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

activity_add_edit.xml
Tampilan untuk mengisi biodata.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_add_edit"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/txt_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:visibility="gone"/>

<EditText
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input Name"/>

34
<EditText
android:id="@+id/txt_address"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input Address"/>

<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/btn_submit"
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"/>

<Button
android:id="@+id/btn_cancel"
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"/>

</LinearLayout>

</LinearLayout>

Agar project terstruktur dan terorganisir, buat 3 paket dengan nama adapter, helper,
dan model. Untuk membuat paket baru , klik kanan pada src=>New=>Package

35
Buat class Adapter.java didalam package adapter dan tambahkan coding seperti
dibawah ini. Class ini berfungsi sebagai menampilkan data seperti id, nama, dan alamat
kemudian ditampilkan ke dalam listview.
Adapter.java

package com.example.yanmed.pertemuan5.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.yanmed.pertemuan5.R;
import com.example.yanmed.pertemuan5.model.Data;

import java.util.List;;

public class Adapter extends BaseAdapter {


private Activity activity;
private LayoutInflater inflater;
private List<Data> items;

public Adapter(Activity activity, List<Data> items) {


this.activity = activity;
this.items = items;
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int location) {
return items.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup
parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);

36
TextView id = (TextView) convertView.findViewById(R.id.id);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView address = (TextView)
convertView.findViewById(R.id.address);

Data data = items.get(position);

id.setText(data.getId());
name.setText(data.getName());
address.setText(data.getAddress());

return convertView;
}
}

Buat class Data.java didalam package model dan tambahkan coding seperti dibawah ini.
Class ini berfungsi sebagai membuat objek. Objek ini berisi informasi seperti id, nama,
dan alamat.
Data.java
package com.example.yanmed.pertemuan5.model;
public class Data {
private String id, name, address;

public Data() {
}
public Data(String id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}
}

37
Buat class DbHelper.java didalam package helper dan tambahkan coding seperti
dibawah ini. Class ini berfungsi sebagai pengeloala query SQL CRUD.
DbHelper.java

package com.example.yanmed.pertemuan5.helper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;

public class DbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;

static final String DATABASE_NAME = "kuncorosqlite.db";

public static final String TABLE_SQLite = "sqlite";

public static final String COLUMN_ID = "id";


public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";

public DbHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " +
TABLE_SQLite + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +
COLUMN_NAME + " TEXT NOT NULL, " +
COLUMN_ADDRESS + " TEXT NOT NULL" +
" )";

db.execSQL(SQL_CREATE_MOVIE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);
onCreate(db);
}

public ArrayList<HashMap<String, String>> getAllData() {


ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM " + TABLE_SQLite;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
}

38
do {
HashMap<String, String> map = new HashMap<String,
String>();
map.put(COLUMN_ID, cursor.getString(0));
map.put(COLUMN_NAME, cursor.getString(1));
map.put(COLUMN_ADDRESS, cursor.getString(2));
wordList.add(map);
} while (cursor.moveToNext());
}

Log.e("select sqlite ", "" + wordList);

database.close();
return wordList;
}

public void insert(String name, String address) {


SQLiteDatabase database = this.getWritableDatabase();
String queryValues = "INSERT INTO " + TABLE_SQLite + " (name,
address) " +
"VALUES ('" + name + "', '" + address + "')";

Log.e("insert sqlite ", "" + queryValues);


database.execSQL(queryValues);
database.close();
}

public void update(int id, String name, String address) {


SQLiteDatabase database = this.getWritableDatabase();

String updateQuery = "UPDATE " + TABLE_SQLite + " SET "


+ COLUMN_NAME + "='" + name + "', "
+ COLUMN_ADDRESS + "='" + address + "'"
+ " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}

public void delete(int id) {


SQLiteDatabase database = this.getWritableDatabase();

String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " +


COLUMN_ID + "=" + "'" + id + "'";
Log.e("update sqlite ", updateQuery);
database.execSQL(updateQuery);
database.close();
}

39
Buka class MainActivity.java dan tambahkan coding seperti dibawah ini. Class ini
berfungsi menampilkan data biodata dalam listview. Floating Action Button di gunakan
untuk memanggil Form diodata yang berisi nama dan alamat. Menekan lama listview
akan menampilkan pilihan edit dan delete.
MainActivity.java

package com.example.yanmed.pertemuan5;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.yanmed.pertemuan5.adapter.Adapter;
import com.example.yanmed.pertemuan5.helper.DbHelper;
import com.example.yanmed.pertemuan5.model.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

ListView listView;
AlertDialog.Builder dialog;
List<Data> itemList = new ArrayList<Data>();
Adapter adapter;
DbHelper SQLite = new DbHelper(this);

public static final String TAG_ID = "id";


public static final String TAG_NAME = "name";
public static final String TAG_ADDRESS = "address";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SQLite = new DbHelper(getApplicationContext());
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
listView = (ListView) findViewById(R.id.list_view);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
AddEdit.class);
startActivity(intent);
}
});

40
adapter = new Adapter(MainActivity.this, itemList);
listView.setAdapter(adapter);

// long press listview to show edit and delete


listView.setOnItemLongClickListener(new
AdapterView.OnItemLongClickListener() {

@Override
public boolean onItemLongClick(final AdapterView<?> parent, View
view,
final int position, long id) {
// TODO Auto-generated method stub
final String idx = itemList.get(position).getId();
final String name = itemList.get(position).getName();
final String address = itemList.get(position).getAddress();

final CharSequence[] dialogitem = {"Edit", "Delete"};


dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(true);
dialog.setItems(dialogitem, new
DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case 0:
Intent intent = new
Intent(MainActivity.this, AddEdit.class);
intent.putExtra(TAG_ID, idx);
intent.putExtra(TAG_NAME, name);
intent.putExtra(TAG_ADDRESS, address);
startActivity(intent);
break;
case 1:
SQLite.delete(Integer.parseInt(idx));
itemList.clear();
getAllData();
break;
}
}
}).show();
return false;
}
});

getAllData();

41
private void getAllData() {
ArrayList<HashMap<String, String>> row = SQLite.getAllData();

for (int i = 0; i < row.size(); i++) {


String id = row.get(i).get(TAG_ID);
String poster = row.get(i).get(TAG_NAME);
String title = row.get(i).get(TAG_ADDRESS);

Data data = new Data();

data.setId(id);
data.setName(poster);
data.setAddress(title);

itemList.add(data);
}

adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
super.onResume();
itemList.clear();
getAllData();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

42
Buat class baru AddEdit.java dan tambahkan coding seperti dibawah ini. Class ini
berfungsi menampilkan EditText nama dan alamat. Didalam class ini juga terdapat fungsi
untuk insert dan update data ke SQLite database.
AddEdit.java

package com.example.yanmed.pertemuan5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yanmed.pertemuan5.helper.DbHelper;

public class AddEdit extends AppCompatActivity {

EditText txt_id, txt_name, txt_address;


Button btn_submit, btn_cancel;
DbHelper SQLite = new DbHelper(this);
String id, name, address;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

txt_id = (EditText) findViewById(R.id.txt_id);


txt_name = (EditText) findViewById(R.id.txt_name);
txt_address = (EditText) findViewById(R.id.txt_address);
btn_submit = (Button) findViewById(R.id.btn_submit);
btn_cancel = (Button) findViewById(R.id.btn_cancel);

id = getIntent().getStringExtra(MainActivity.TAG_ID);
name = getIntent().getStringExtra(MainActivity.TAG_NAME);
address = getIntent().getStringExtra(MainActivity.TAG_ADDRESS);

if (id == null || id == "") {


setTitle("Add Data");
} else {
setTitle("Edit Data");
txt_id.setText(id);
txt_name.setText(name);
txt_address.setText(address);
}

43
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (txt_id.getText().toString().equals("")) {
save();
} else {
edit();
}
} catch (Exception e){
Log.e("Submit", e.toString());
}}});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
blank();
finish();
}});}

@Override
public void onBackPressed() {
finish(); }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
blank();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item); }}
// Make blank all Edit Text
private void blank() {
txt_name.requestFocus();
txt_id.setText(null);
txt_name.setText(null);
txt_address.setText(null);}
// Save data to SQLite database
private void save() {
if (String.valueOf(txt_name.getText()).equals(null) ||
String.valueOf(txt_name.getText()).equals("") ||
String.valueOf(txt_address.getText()).equals(null) ||
String.valueOf(txt_address.getText()).equals("")) {
Toast.makeText(getApplicationContext(),
"Please input name or address ...",
Toast.LENGTH_SHORT).show();
} else {
SQLite.insert(txt_name.getText().toString().trim(),
txt_address.getText().toString().trim());
blank();
finish();
}}

44
// Update data in SQLite database
private void edit() {
if (String.valueOf(txt_name.getText()).equals(null) ||
String.valueOf(txt_name.getText()).equals("") ||
String.valueOf(txt_address.getText()).equals(null) ||
String.valueOf(txt_address.getText()).equals("")) {
Toast.makeText(getApplicationContext(),
"Please input name or address ...",
Toast.LENGTH_SHORT).show();
} else {

SQLite.update(Integer.parseInt(txt_id.getText().toString().trim()),
txt_name.getText().toString().trim(),
txt_address.getText().toString().trim());
blank();
finish();
}
}

Tambahkan activity di AndroidManifest

<activity android:name=".AddEdit"/>

Run Aplikasinya

45
DAFTAR PUSTAKA

http://www.JAVA-indo.or.id/J2ME.html (diakses tanggal 12 Juni 2011)


http://www.slideshare.net/ptsumaye/-javapemerograman-7456329 (diakses 2
november 2010)
http://en.wikipedia.org/wiki/J2ME (diakses 19 Desember 2011)
ocw.gunadarma.ac.id/course/computer.../database-rms/view (diakses 19 Desember
2011)
ftp.gunadarma.ac.id/...Sistem%20Informasi/SISTEM%2interface (diakses 17
Desember 2011)
www.siliconfareast.com/antarmuka.htmTembolok - Mirip (diakses 17 Desember
2011)
www.innovativeelectronics.co/envent-handling.html (diakses 19 Desember 2011)
zainal777.blogspot.com/2010/12/pemerograman-mobile-display.html (diakses 19
Desember 2011)
www.aaroncake.net/canvas-display.html (diakses 19 Desember 2011)

46

You might also like