import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.IOException;
public class DemoCariRecord extends MIDlet
implements CommandListener {
private Display display;
private List list;
private TextField tfCari;
private RecordStore rs = null;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
private final Command cmdKembali =
new Command("Kembali", Command.BACK, 1);
private final Command cmdPencarian =
new Command("Pencarian", Command.SCREEN, 1);
private final Command cmdCari =
new Command("Cari", Command.OK, 1);
public DemoCariRecord() {
display = Display.getDisplay(this);
list = new List(null, Choice.IMPLICIT);
list.addCommand(cmdKeluar);
list.addCommand(cmdPencarian);
list.setCommandListener(this);
try {
rs = RecordStore.openRecordStore("DBNama", true);
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
}
public void startApp() {
try {
if (rs.getNumRecords() < 1) {
tambahData("Kiki");
tambahData("Fira");
tambahData("Dede");
tambahData("Mira");
tambahData("Aryo");
tambahData("Dona");
}
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
tampilkanData(false);
display.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdKembali) {
display.setCurrent(list);
tampilkanData(false);
} else if (c == cmdPencarian) {
Form form = new Form("Cari Nama");
tfCari = new TextField("Masukkan karakter pertama", null,
1, TextField.ANY);
form.append(tfCari);
form.addCommand(cmdCari);
form.addCommand(cmdKembali);
form.setCommandListener(this);
display.setCurrent(form);
} else if (c == cmdCari) {
tampilkanData(true);
}
}
public void tambahData(String nama) {
byte[] temp = null;
try {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(nama);
temp = baos.toByteArray();
} catch (IOException ioe) {
ioe.printStackTrace();
}
try {
rs.addRecord(temp, 0, temp.length);
} catch (RecordStoreNotOpenException rsnoe) {
rsnoe.printStackTrace();
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
}
public void tampilkanData(boolean filter) {
byte[] temp = null;
RecordEnumeration re = null;
list.deleteAll();
try {
if (filter) {
String s = tfCari.getString().toLowerCase();
CariNama cn = new CariNama(s);
re = rs.enumerateRecords(cn, null, false);
list.setTitle("Hasil Pencarian");
list.removeCommand(cmdPencarian);
list.removeCommand(cmdKeluar);
list.addCommand(cmdKembali);
} else {
re = rs.enumerateRecords(null, null, false);
list.setTitle("Daftar Nama");
list.addCommand(cmdPencarian);
list.addCommand(cmdKeluar);
list.removeCommand(cmdKembali);
}
while (re.hasNextElement()) {
int i = re.nextRecordId();
temp = rs.getRecord(i);
ByteArrayInputStream bais =
new ByteArrayInputStream(temp);
DataInputStream dis = new DataInputStream(bais);
try {
String nama = dis.readUTF();
list.append(nama, null);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
display.setCurrent(list);
} catch (InvalidRecordIDException invID) {
invID.printStackTrace();
} catch (RecordStoreNotOpenException rsnoe) {
rsnoe.printStackTrace();
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
display.setCurrent(list);
}
// inner class untuk mencari data
class CariNama implements RecordFilter {
private String karakterAwal;
public CariNama(String karakterAwal) {
this.karakterAwal = karakterAwal.toLowerCase();
}
public boolean matches(byte[] candidate) {
String s =
new String(candidate).toLowerCase().substring(2);
return s.startsWith(karakterAwal);
}
}
}
No comments:
Post a Comment