Sei sulla pagina 1di 16

LAPORAN PRAKTIKUM KOMPUTASI MOBILE

MODUL KE 1 dan 2
SQLite Android




Winda Andrini Wulandari
(09560054)






LABORATORIUM JARINGAN KOMPUTER
PROGRAM STUDI TEKNIK INFORMATIKA
FAKULTAS TEKNIK
UNIVERSITAS MUHAMMADIYAH MALANG
2013/2014


BukuTelpon.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;


import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
* @author Anuja
*/
public class BukuTelpon extends MIDlet implements CommandListener,
RecordComparator, ItemStateListener, RecordFilter, ItemCommandListener {

private Display display;
private ChoiceGroup searchChoice;
private Form searchForm;
private TextField searchTxtField;
private Command exitCmd;
private Command addContactCmd;
private Form addContactForm;
private TextField nameTxtField;
private TextField numberTxtField;
private Command backCmd;
private Command saveCmd;
private String nameStr;
private String numberStr;
private Alert addAlert;
private RecordStore openRecStore;
private Vector vectorArray;

private String filterStr = "";

private Form changeContactForm;
private Command viewCmd;
private TextField changeNameTxtField;
private TextField changeNumberTxtField;
private Command changeBackCmd;
private Command changeUpdateCmd;
private Command changeDelCmd;
private Alert warningAlert;

public void startApp() {
display = Display.getDisplay(this);

// --------------------- Search Contact Form ---------------------------

searchForm = new Form("Cari Kontak");
searchTxtField = new TextField("Masukkan Nama", "", 30,
TextField.ANY);
searchForm.append(searchTxtField);

searchChoice = new ChoiceGroup("", Choice.EXCLUSIVE);
//searchChoice.append("Anuja", null);
searchForm.append(searchChoice);

exitCmd = new Command("Keluar", Command.EXIT, 7);
searchForm.addCommand(exitCmd);
addContactCmd = new Command("Tambah", Command.OK, 4);
searchForm.addCommand(addContactCmd);

viewCmd = new Command("Tampilkan", Command.OK, 4);
searchChoice.addCommand(viewCmd);

searchChoice.setItemCommandListener(this);

searchForm.setItemStateListener(this);

searchForm.setCommandListener(this);
display.setCurrent(searchForm);

//-------------------- Add Contact Form --------------------------------

addContactForm = new Form("Tambah Kontak");
nameTxtField = new TextField("Nama", "", 80, TextField.ANY);
addContactForm.append(nameTxtField);
numberTxtField = new TextField("Nomor", "", 20,
TextField.PHONENUMBER);
addContactForm.append(numberTxtField);

backCmd = new Command("Kembali", Command.BACK, 2);
addContactForm.addCommand(backCmd);
saveCmd = new Command("Simpan", Command.OK, 4);
addContactForm.addCommand(saveCmd);

addContactForm.setCommandListener(this);

//----------------------- Change Contact Form ----------------------------

changeContactForm = new Form("Edit Kontak");
changeNameTxtField = new TextField("", "", 80, TextField.ANY);
changeContactForm.append(changeNameTxtField);
changeNumberTxtField = new TextField("", "", 20,
TextField.PHONENUMBER);
changeContactForm.append(changeNumberTxtField);

changeBackCmd = new Command("Kembali", Command.BACK, 2);
changeContactForm.addCommand(changeBackCmd);
changeUpdateCmd = new Command("Perbarui", Command.OK, 4);
changeContactForm.addCommand(changeUpdateCmd);
changeDelCmd = new Command("Hapus", Command.OK, 4);
changeContactForm.addCommand(changeDelCmd);

changeContactForm.setCommandListener(this);

//-------------------- Open a RecordStore ------------------------------

try {
openRecStore = RecordStore.openRecordStore("Kontak", true);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}

loadContacts();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
notifyDestroyed();
} else if (c == addContactCmd) {
nameTxtField.setString("");
numberTxtField.setString("");
display.setCurrent(addContactForm);
} else if (c == backCmd) {
searchTxtField.setString("");
display.setCurrent(searchForm);
} else if (c == saveCmd) {

//------------------ Get the inputs ------------------------
nameStr = nameTxtField.getString();
numberStr = numberTxtField.getString();
String fullDetails = nameStr + "*" + numberStr;

addAlert = new Alert("Tambah ke kontak");
loadContacts();
display.setCurrent(addAlert, searchForm);

searchTxtField.setString("");

byte[] byteArray = fullDetails.getBytes();

try {
openRecStore.addRecord(byteArray, 0, fullDetails.length());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}

loadContacts();
}else if(c == changeBackCmd){
loadContacts();
display.setCurrent(searchForm);
}else if(c == changeUpdateCmd){
int getSelInd = searchChoice.getSelectedIndex();
Contact getConInfo = (Contact) vectorArray.elementAt(getSelInd);
int recordId = getConInfo.getContactId();

String changeName = changeNameTxtField.getString();
String changeNumber = changeNumberTxtField.getString();

String changeFullDetails = changeName + "*" + changeNumber;
byte[] editRec = changeFullDetails.getBytes();

try {
openRecStore.setRecord(recordId, editRec, 0, editRec.length);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}

loadContacts();
warningAlert = new Alert("Berhasil Diperbarui", "", null,
AlertType.INFO);
display.setCurrent(warningAlert, searchForm);

}else if(c == changeDelCmd){
int getSelInd = searchChoice.getSelectedIndex();
Contact getConInfo = (Contact) vectorArray.elementAt(getSelInd);

int recordId = getConInfo.getContactId();

try {
openRecStore.deleteRecord(recordId);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
loadContacts();
warningAlert = new Alert("Hapus !", "", null,
AlertType.CONFIRMATION);
display.setCurrent(warningAlert, searchForm);
}
}

private void loadContacts() {
searchChoice.deleteAll();

vectorArray = new Vector();
vectorArray.removeAllElements();

try {
RecordEnumeration enuRec = openRecStore.enumerateRecords(this,
this, true);

while (enuRec.hasNextElement()) {
try {
int recordId = enuRec.nextRecordId();
byte[] nextRec = openRecStore.getRecord(recordId);
String nextRecStr = new String(nextRec);
String takeName = nextRecStr.substring(0,
nextRecStr.indexOf("*"));
String takeNumber =
nextRecStr.substring(nextRecStr.indexOf("*") + 1, nextRecStr.length());

searchChoice.append(takeName, null);

Contact conDet = new Contact();
conDet.setContactId(recordId);
conDet.setContactName(takeName);
conDet.setContactNumber(takeNumber);
vectorArray.addElement(conDet);

} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
}

public int compare(byte[] rec1, byte[] rec2) {

String data1 = new String(rec1);
String data2 = new String(rec2);


int x = data1.compareTo(data2);

if(x == 0){
return RecordComparator.EQUIVALENT;
}else if(x < 0){
return RecordComparator.PRECEDES;
}else{
return RecordComparator.FOLLOWS;
}
}

public void itemStateChanged(Item item) {
if(item == searchTxtField){
filterStr = searchTxtField.getString();
loadContacts();
}
}

public boolean matches(byte[] candidate) {

String candi = new String(candidate);
if (candi.startsWith(filterStr)) {
return true;
}
return false;
}

public void commandAction(Command c, Item item) {
if(c == viewCmd){
int getSelInd = searchChoice.getSelectedIndex();
Contact getConInfo = (Contact) vectorArray.elementAt(getSelInd);

changeNameTxtField.setString(getConInfo.getContactName());
changeNumberTxtField.setString(getConInfo.getContactNumber());
display.setCurrent(changeContactForm);
}
}
}



Contact.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;


class Contact{
private int contactId;
private String contactName;
private String contactHome;
private String contactNumber;
private String contactAddress;

/**
* @return the contactId
*/
public int getContactId() {
return contactId;
}

/**
* @param contactId the contactId to set
*/
public void setContactId(int contactId) {
this.contactId = contactId;
}

/**
* @return the contactName
*/
public String getContactName() {
return contactName;
}

/**
* @param contactName the contactName to set
*/
public void setContactName(String contactName) {
this.contactName = contactName;
}

/**
* @return the contactHome
*/
public String getContactHome() {
return contactHome;
}

/**
* @param contactHome the contactHome to set
*/
public void setContactHome(String contactHome) {
this.contactHome = contactHome;
}

/**
* @return the contactNumber
*/
public String getContactNumber() {
return contactNumber;
}

/**
* @param contactNumber the contactNumber to set
*/
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}

/**
* @return the contactAddress
*/
public String getContactAddress() {
return contactAddress;
}

/**
* @param contactAddress the contactAddress to set
*/
public void setContactAddress(String contactAddress) {
this.contactAddress = contactAddress;
}
}

Potrebbero piacerti anche