Java Tutorial/J2ME/RecordListener
Версия от 17:44, 31 мая 2010; (обсуждение)
implements RecordListener
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;
class TestDB implements RecordListener {
public void recordAdded(RecordStore rs, int id) {
try {
System.out.println(rs.getName() + " added record " + id);
} catch (RecordStoreNotOpenException e) {
}
}
public void recordChanged(RecordStore rs, int id) {
try {
System.out.println(rs.getName() + " changed record " + id);
} catch (RecordStoreNotOpenException e) {
}
}
public void recordDeleted(RecordStore rs, int id) {
try {
System.out.println(rs.getName() + " removed record " + id);
} catch (RecordStoreNotOpenException e) {
}
}
}
class TestFilter implements RecordFilter {
public boolean matches(byte[] rec) {
String r = new String(rec);
return ((r.charAt(0) == "a") || (r.charAt(0) == "A"));
}
}
class TestComparator implements RecordComparator {
public int compare(byte[] rec1, byte[] rec2) {
String r1 = new String(rec1);
String r2 = new String(rec2);
if (r1.rupareTo(r2) > 0)
return (RecordComparator.FOLLOWS);
else if (r1.rupareTo(r2) < 0)
return (RecordComparator.PRECEDES);
else
return (RecordComparator.EQUIVALENT);
}
}
public class J2METestDBTest extends MIDlet {
public void startApp() {
try {
RecordStore anRMS = RecordStore.openRecordStore("TestRMS", true);
anRMS.addRecordListener(new TestDB());
String test = "This is a test";
byte[] b = test.getBytes();
anRMS.addRecord(b, 8, 6);
anRMS.addRecord(b, 5, 2);
RecordComparator rc = new TestComparator();
byte[] r1 = anRMS.getRecord(1);
byte[] r2 = anRMS.getRecord(2);
System.out.println(rc.rupare(r1, r2));
} catch (Exception e) {
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}