ola pessoal estou com problemas na hora de alterar inseri o codigo mas ele nao esta alterando alguem poderia consertar o meu codigo eu me postar um exemplo que funcione?
estou no rms o le, criar, excluir ta funcionando so nao ta o alterar
abraço
segue meu codigo inteiro
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.rms.*;
import java.util.Vector;
/**
*
* @author home
* @version
*/
public class Agenda extends MIDlet implements CommandListener{
public Display odisplay;
public Form frmAgenda;
public List lista;
public TextField tf_nome, tf_fone;
public Command cmd_voltar, cm_novo, cm_salvar, cm_excluir, cm_editar, cm_voltar_lista, cm_atualizar;
public RecordStore rsNomes;
public RecordEnumeration reNomes;
public Vector vecId = new Vector();
public void startApp() {
odisplay = Display.getDisplay(this);
frmAgenda = new Form("cadastrar contatos");
lista = new List("lista de fones",List.EXCLUSIVE);
odisplay.setCurrent(lista);
tf_nome = new TextField("nome:","",20,TextField.ANY);
tf_fone = new TextField("fone:","",8,TextField.PHONENUMBER);
cmd_voltar = new Command("Voltar",Command.BACK,1);
cm_novo = new Command("novo",Command.ITEM,1);
cm_salvar = new Command("salvar",Command.ITEM,2);
cm_excluir = new Command("excluir",Command.ITEM,3);
cm_editar = new Command("editar", Command.ITEM,4);
cm_atualizar = new Command("atualizar", Command.ITEM,5);
cm_voltar_lista = new Command("voltar lista", Command.BACK,1);
frmAgenda.append(tf_nome);
frmAgenda.append(tf_fone);
lista.addCommand(cmd_voltar);
lista.addCommand(cm_novo);
// lista.addCommand(cm_salvar);
lista.addCommand(cm_excluir);
lista.addCommand(cm_editar);
frmAgenda.addCommand(cm_salvar);
frmAgenda.addCommand(cm_voltar_lista);
frmAgenda.addCommand(cm_atualizar);
frmAgenda.setCommandListener(this);
lista.setCommandListener(this);
criabd();
leitura();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void criabd()
{
try
{
rsNomes = RecordStore.openRecordStore("bc_agenda", true);
System.out.println("nome: "+rsNomes.getName());
System.out.println("tamanho: "+rsNomes.getSizeAvailable());
System.out.println("usado: "+rsNomes.getSize());
System.out.println("registros: "+rsNomes.getNumRecords());
}
catch(Exception erro)
{
System.out.print("erro");
erro.printStackTrace();
}
}
public void inserirBD()
{
try
{
ByteArrayOutputStream baos_ent = new ByteArrayOutputStream(); //cria um novo byte
DataOutputStream dosarqsai = new DataOutputStream(baos_ent);
dosarqsai.writeUTF(tf_nome.getString());
dosarqsai.writeUTF(tf_fone.getString());
dosarqsai.flush();
byte [] dados = baos_ent.toByteArray();
rsNomes.addRecord(dados,0,dados.length);
baos_ent.close();
dosarqsai.close();
}
catch(Exception erro)
{
System.out.println("erro ao inserir");
}
}
public void leitura()
{
try
{
while(lista.size()>0);
lista.deleteAll();
ByteArrayInputStream bais_dentro = null;
DataInputStream dadodata = null;
reNomes = rsNomes.enumerateRecords(null,null,false);
int id = 0;
byte [] dados = null;
vecId.removeAllElements();
while(reNomes.hasNextElement())
{
id = reNomes.nextRecordId();
vecId.addElement(""+id);
dados = rsNomes.getRecord(id);
bais_dentro = new ByteArrayInputStream(dados);
dadodata = new DataInputStream(bais_dentro);
lista.append(dadodata.readUTF()+ " - " +dadodata.readUTF(), null);
}
bais_dentro.close();
dadodata.close();
}
catch(Exception erro)
{
System.out.println("erro");
erro.printStackTrace();
}
}
public void update() {
try {
while(reNomes.hasNextElement())
{
int id = reNomes.nextRecordId();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(tf_nome.getString());
dos.flush();
byte[] data = baos.toByteArray();
rsNomes.setRecord(id, data, 0, data.length);
}
}
catch (Exception e)
{
System.out.println("-- Erro de IO");
e.printStackTrace();
}
}
public void excluir()
{
try
{
int indice = lista.getSelectedIndex();
rsNomes.deleteRecord(Integer.parseInt(vecId.elementAt(indice).toString()));
}
catch(Exception e)
{
System.out.println("erro exclusao");
}
}
public void commandAction(Command c, Displayable d) {
if(c==cm_novo)
{
odisplay.setCurrent(frmAgenda);
tf_nome.setString("");
tf_fone.setString("");
}
else if(c==cmd_voltar)
{
destroyApp(true);
notifyDestroyed();
}
else if(c==cm_voltar_lista)
{
odisplay.setCurrent(lista);
}
else if(c==cm_salvar)
{
lista.append(tf_nome.getString()+ " - " +tf_fone.getString(), null);
odisplay.setCurrent(lista);
inserirBD();
}
else if(c==cm_atualizar)
{
odisplay.setCurrent(lista);
update();
}
else if(c==cm_excluir)
{
excluir();
}
else if(c==cm_editar)
{
String nomeFone = lista.getString(lista.getSelectedIndex());
tf_nome.setString(nomeFone.substring(0,nomeFone.indexOf("-")-1));
tf_fone.setString(nomeFone.substring(nomeFone.indexOf("-")+2));
odisplay.setCurrent(frmAgenda);
}
}
}