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 DemoUrutRecord extends MIDlet
implements CommandListener {
private Display display;
private List list;
private RecordStore rs = null;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
private final Command cmdUrutkan =
new Command("Urutkan", Command.SCREEN, 1);
public DemoUrutRecord() {
display = Display.getDisplay(this);
list = new List("Daftar Nama", Choice.IMPLICIT);
list.addCommand(cmdKeluar);
list.addCommand(cmdUrutkan);
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");
}
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
tampilkanData(false); // menampilkan data tanpa diurutkan
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 == cmdUrutkan) {
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 urut) {
byte[] temp = null;
RecordEnumeration re = null;
list.deleteAll();
try {
if (urut) {
UrutNama un = new UrutNama();
re = rs.enumerateRecords(null, un, false);
} else {
re = rs.enumerateRecords(null, null, false);
}
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();
}
}
// inner class untuk mengurutkan data
class UrutNama implements RecordComparator {
public int compare(byte[] record1, byte[] record2) {
String nama1 = new String(record1);
String nama2 = new String(record2);
int hasil = nama1.compareTo(nama2);
if (hasil < 0) {
return RecordComparator.PRECEDES;
} else if (hasil > 0) {
return RecordComparator.FOLLOWS;
} else {
return RecordComparator.EQUIVALENT;
}
}
}
}
No comments:
Post a Comment