galera bom dia a todos?
Estou com uma enorme dificuldade de entender a persistência em J2Me,pois faltei a aula de persistência na faculdade…estou com esse código abaixo,sem saber como Alterar,excluir e consultar.gostaria da ajuda de vocês para implementação.
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.;
import javax.microedition.lcdui.;
public class Agenda extends MIDlet implements CommandListener {
private Display tela;
private Form form;
private TextField nome, tel;
private Command sair, cadastrar, listar, alterar, excluir, consultar;
private RecordStore rs;
public Agenda() {}
public void destroyApp(boolean b) throws MIDletStateChangeException {}
public void pauseApp() {}
public void startApp() throws MIDletStateChangeException {
tela = Display.getDisplay(this);
form = new Form ("Agenda");
nome = new TextField("Nome:","",20,TextField.ANY);
tel = new TextField ("Tel:", "", 11, TextField.ANY);
sair = new Command("Sair",Command.EXIT,1);
cadastrar = new Command ("Cadastrar", Command.SCREEN,1);
listar = new Command ("listar", Command.SCREEN,1);
alterar = new Command("Alterar", Command.SCREEN,1);
excluir = new Command ("Excluir", Command.SCREEN,1);
consultar = new Command ("Consultar", Command.SCREEN,1);
form.append(nome);
form.append(tel);
form.addCommand(sair);
form.addCommand(cadastrar);
form.addCommand(listar);
form.addCommand(alterar);
form.addCommand(excluir);
form.addCommand(consultar);
form.setCommandListener(this);
tela.setCurrent(form);
}
public void commandAction(Command C, Displayable D) {
if (C==sair){
notifyDestroyed();
}
if (C==cadastrar){
try{
rs=RecordStore.openRecordStore("Agenda", true);// o true,serve para criar o arquivo caso ele não exista.
byte[]bnome = (nome.getString()).getBytes();
byte[]btel = (tel.getString()).getBytes();
rs.addRecord(bnome, 0, bnome.length);
rs.addRecord(btel, 0, btel.length);
Alert mensagem =new Alert("Agenda 2008","Cliente inserido com sucesso",null,null);
mensagem.setTimeout(2000);
tela.setCurrent(mensagem);
rs.closeRecordStore();
}catch (Exception e){}
}
if (C==listar){
String aux="";
try{
rs=RecordStore.openRecordStore("Agenda", true);
try{
for (int i=1;i<rs.getNextRecordID();i+=2)
{//i+=2: identifica os dois campos da tela, de dois em dois. rsrs
String snome = new String (rs.getRecord(i));
String stel = new String (rs.getRecord(i+1));
aux = aux+"Nome:"+snome+ " Tel:"+stel+"\n";
}
}catch(Exception e){}
Alert msg = new Alert("Lista",aux,null, AlertType.INFO);
msg.setTimeout(3000);
tela.setCurrent(msg);
}
catch(Exception e){
}
}
if(C==Alterar){}
if(C==Excluir){}
if(C==Consultar){}
Valeu obrigado galera…