1. Buat Project Baru
2. Desain interface baru untuk tampilan awal program
3. Buat Layout baru dengan nama dialogview dengan tampilan seperti berikut :
5. Buat Layout baru dengan nama menu:
6. Buat Layout baru dengan nama viewdata:
7. Buat class baru dengan nama Barang.java dengan sourcode:
public class Barang {private long id;
private String nama_barang;
private String merk_barang;
private String harga_barang;
public Barang()
{
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNama_barang() {
return nama_barang;
}
public void setNama_barang(String nama_barang) {
this.nama_barang = nama_barang;
}
public String getMerk_barang() {
return merk_barang;
}
public void setMerk_barang(String merk_barang) {
this.merk_barang = merk_barang;
}
public String getHarga_barang() {
return harga_barang;
}
public void setHarga_barang(String harga_barang) {
this.harga_barang = harga_barang;
}
@Override
public String toString()
{
return "Barang "+ nama_barang +" "+ merk_barang + " "+harga_barang;
}
}
8. Buat class baru dengan nama CreateData.java :
package amikom.si12a.databasesi12a;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Bundle;
import android.widget.Toast;
public class CreateData extends Activity implements OnClickListener {
//inisilisasi elemen-elemen pada layout
private Button buttonSubmit;
private EditText edNama;
private EditText edMerk;
private EditText edHarga;
//inisialisasi kontroller/Data Source
private DBDataSource dataSource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_data);
buttonSubmit = (Button) findViewById(R.id.buttom_submit);
buttonSubmit.setOnClickListener(this);
edNama = (EditText) findViewById(R.id.nama_barang);
edHarga = (EditText) findViewById(R.id.harga_barang);
edMerk = (EditText) findViewById(R.id.merk_barang);
// instanstiasi kelas DBDataSource
dataSource = new DBDataSource(this);
//membuat sambungan baru ke database
dataSource.open();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Inisialisasi data barang
String nama = null;
String merk = null;
String harga = null;
@SuppressWarnings("unused")
//inisialisasi barang baru (masih kosong)
Barang barang = null;
if(edNama.getText()!=null && edMerk.getText()!=null && edHarga.getText()!=null)
{
/* jika field nama, merk, dan harga tidak kosong
* maka masukkan ke dalam data barang*/
nama = edNama.getText().toString();
merk = edMerk.getText().toString();
harga = edHarga.getText().toString();
}
switch(v.getId())
{
case R.id.buttom_submit:
// insert data barang baru
barang = dataSource.createBarang(nama, merk, harga);
//konfirmasi kesuksesan
Toast.makeText(this, "masuk Barang\n" +
"nama" + barang.getNama_barang() +
"merk" + barang.getMerk_barang() +
"harga" + barang.getHarga_barang(), Toast.LENGTH_LONG).show();
break;
}
}
}
9. Buat class baru dengan nama EditData.java:
package amikom.si12a.databasesi12a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EditData extends Activity implements OnClickListener {
private DBDataSource dataSource;
private long id;
private String harga;
private String merk;
private String nama;
private EditText edNama;
private EditText edHarga;
private EditText edMerk;
private TextView txId;
private Button btnSave;
private Button btnCancel;
private Barang barang;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editdata);
//inisialisasi variabel
edNama = (EditText) findViewById(R.id.editText_nama);
edHarga = (EditText) findViewById(R.id.editText_harga);
edMerk = (EditText) findViewById(R.id.editText_merk);
txId = (TextView) findViewById(R.id.text_id_barang);
//buat sambungan baru ke database
dataSource = new DBDataSource(this);
dataSource.open();
// ambil data barang dari extras
Bundle bun = this.getIntent().getExtras();
id = bun.getLong("id");
harga = bun.getString("harga");
merk = bun.getString("merk");
nama = bun.getString("nama");
//masukkan data-data barang tersebut ke field editor
txId.append(String.valueOf(id));
edNama.setText(nama);
edHarga.setText(harga);
edMerk.setText(merk);
//set listener pada tombol
btnSave = (Button) findViewById(R.id.button_save_update);
btnSave.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.button_cancel_update);
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
// apabila tombol save diklik (update barang)
case R.id.button_save_update :
barang = new Barang();
barang.setHarga_barang(edHarga.getText().toString());
barang.setNama_barang(edNama.getText().toString());
barang.setMerk_barang(edMerk.getText().toString());
barang.setId(id);
dataSource.updateBarang(barang);
Intent i = new Intent(this, ViewData.class);
startActivity(i);
EditData.this.finish();
dataSource.close();
break;
// apabila tombol cancel diklik, finish activity
case R.id.button_cancel_update :
finish();
dataSource.close();
break;
}
}
}
10. Buat class baru dengan nama Menu.java dengan source code:
package amikom.si12a.databasesi12a;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class Menu extends Activity implements OnClickListener {
private Button bTambah;
private Button bLihat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
bTambah = (Button) findViewById(R.id.button_tambah);
bTambah.setOnClickListener(this);
bLihat = (Button) findViewById(R.id.button_view);
bLihat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button_tambah :
Intent i = new Intent(this, CreateData.class);
startActivity(i);
break;
case R.id.button_view :
Intent i2 = new Intent(this, ViewData.class);
startActivity(i2);
break;
}
}
}
11. Buat class dengan nama ViewData.java dengan source code:
package amikom.si12a.databasesi12a;
import java.util.ArrayList;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ViewData extends ListActivity implements OnItemLongClickListener {
//inisialisasi kontroller
private DBDataSource dataSource;
//inisialisasi arraylist
private ArrayList<Barang> values;
private Button editBarang,deleteBarang;
//menampilkan data
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdata);
dataSource = new DBDataSource(this);
// buka kontroller
dataSource.open();
// ambil semua data barang
values = dataSource.getAllBarang();
// masukkan data barang ke array adapter
ArrayAdapter<Barang> adapter = new ArrayAdapter<Barang>(this,
android.R.layout.simple_list_item_1, values);
// set adapter pada list
setListAdapter(adapter);
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setOnItemLongClickListener(this);
}
@Override
public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
final long id) {
// TODO Auto-generated method stub
final Dialog dialog=new Dialog(this);
dialog.setContentView(R.layout.dialogview);
dialog.setTitle("PIlih Aksi");
dialog.show();
final Barang b=(Barang) getListAdapter().getItem(pos);
editBarang=(Button)findViewById(R.id.BTNEDIT);
deleteBarang=(Button)findViewById(R.id.BTNHAPUS);
editBarang.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switchToEdit(b.getId());
dialog.dismiss();
}
});
deleteBarang.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dataSource.deleteBarang(b.getId());
dialog.dismiss();
finish();
startActivity(getIntent());
}
});
return true;
}
public void switchToEdit(long id){
Barang b=dataSource.getBarang(id);
Intent i =new Intent(this,EditData.class);
Bundle bun=new Bundle();
bun.putLong("id",b.getId());
bun.putString("nama",b.getNama_barang());
bun.putString("harga",b.getHarga_barang());
i.putExtras(bun);
finale();
startActivity(i);
}
public void finale(){
ViewData.this.finish();
dataSource.close();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
}
12. Edit source code pada Android manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amikom.si12a.databasesi12a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="dbandroid.Menu"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.dbandroid.CreateData"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.dbandroid.Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.dbandroid.ViewData"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.dbandroid.EditData"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
0 komentar:
Post a Comment
Note: only a member of this blog may post a comment.