هذا برنامج قمت بعمله للتدرب على ال RecordStore وهو عبارة عن مفكرة ويمكن تطويره احببت ان اريكم الكود للتدرب على الحفظ والحذف
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class NoteBookMIDlet extends MIDlet implements CommandListener
{
Display display;
TextBox tb;
List l;
String rsname ="Database";
RecordStore rs;
Command e = new Command("Exit", Command.EXIT,0);
Command a = new Command("New",Command.ITEM,1);
Command s = new Command("Save",Command.ITEM,2);
Command b = new Command("Back",Command.BACK,0);
Command d = new Command("Delete",Command.ITEM,1);
public NoteBookMIDlet()
{
display = Display.getDisplay(this);
l = new List("Notes",Choice.IMPLICIT);
try {
rs =RecordStore.openRecordStore(rsname,true);
if(rs.getNumRecords() == 0){System.out.println("RS ist empty");}
else
{
loadfromRecordStore();
}
}
catch (RecordStoreException e)
{
System.out.println("Cannot Open RecordStore");
}
try{
rs.closeRecordStore();
}catch(RecordStoreException e){
e.printStackTrace();
}
}
public void AddTextBox()
{
tb = new TextBox("Add Note","", 150, TextField.ANY);
tb.addCommand(b);
tb.addCommand(e);
tb.addCommand(s);
tb.setCommandListener(this);
display.setCurrent(tb);
}
public void GoToSaveList ()
{
saveEntry(tb.getString());
l.append(tb.getString(),null);
display.setCurrent(l);
}
public void saveEntry(String re)
{
try
{
RecordStore.deleteRecordStore(rsname);
rs = RecordStore.openRecordStore(rsname,true);
byte [] data = re.getBytes();
int len =data.length;
rs.addRecord(data,0,len);
}
catch (RecordStoreException e) {
System.err.println(e.toString());
}
try{
rs.closeRecordStore();
}catch(RecordStoreException e){
e.printStackTrace();
}
}
public void loadfromRecordStore()
{
try
{
rs = RecordStore.openRecordStore(rsname,true);
int num = rs.getNumRecords();
RecordEnumeration enu = rs.enumerateRecords(null,null,true);
byte [] data = null;
while(enu.hasNextElement())
{
data = enu.nextRecord();
String m = new String(data);
l.append(m,null);
}
}
catch (RecordStoreException e) {
e.printStackTrace();
}
try{
rs.closeRecordStore();
}catch(RecordStoreException e){
e.printStackTrace();
}
}
public void deletefromRecordStore( int id)
{
try { rs = RecordStore.openRecordStore(rsname,true);
rs.deleteRecord(id);
rs.closeRecordStore();
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
public void startApp()
{
l.addCommand(a);
l.addCommand(e);
l.addCommand(d);
l.setCommandListener(this);
display.setCurrent(l);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c,Displayable m)
{
if(c == e)
{
destroyApp(false);
notifyDestroyed();
}
else if(c == b)
{
display.setCurrent(l);
}
else if(c==s)
{
GoToSaveList();
}
else if(c==a)
{
AddTextBox();
}
else if(c==d)
{
int h = l.getSelectedIndex();
deletefromRecordStore(h+1);
l.delete(h);
}
}
}