Professional Documents
Culture Documents
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.
Penyusun
ii
TATA TERTIB PRAKTIKUM
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
iv
BAB I
LAYOUT DAN JAVA(Menu Login)
2
Setelah itu masuk ke halaman codingan yang terbagi dengan
beberapa tugasnya, diantaranya untuk codingan java, xml, string,
background dll ada sebelah kiri :
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>
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;
@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.
Setelah di debug maka hasil dari Layout yang telah dibuat akan
tampil seperti gambar berikut ini :
7
BAB II
FORM DATA MAHASISWA
8
Pilih Empty Activity Kemudian Pilih Next
9
Setelah itu masuk ke halaman codingan yang terbagi dengan
beberapa tugasnya, diantaranya untuk codingan java, xml, string,
background dll ada sebelah kiri :
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
<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
<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;
@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());
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;
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
<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;
24
hitung.setOnClickListener(this);
hps.setOnClickListener(this);
save.setOnClickListener(this);
if (!Nilai.isEmpty() || !Huruf.isEmpty()) {
angk.setText(Nilai);
hrf.setText(Huruf);
absen.setText(ABSEN);
}
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
<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);
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());
Save_Nilai = String.valueOf(nilai);
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.
<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.
<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;;
@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);
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;
}
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;
@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);
}
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());
}
database.close();
return wordList;
}
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;
ListView listView;
AlertDialog.Builder dialog;
List<Data> itemList = new ArrayList<Data>();
Adapter adapter;
DbHelper SQLite = new DbHelper(this);
@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);
@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();
@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();
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
id = getIntent().getStringExtra(MainActivity.TAG_ID);
name = getIntent().getStringExtra(MainActivity.TAG_NAME);
address = getIntent().getStringExtra(MainActivity.TAG_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();
}
}
<activity android:name=".AddEdit"/>
Run Aplikasinya
45
DAFTAR PUSTAKA
46