Hello,
I'm learning Java ME and i was building an application that is a simple contact book, but in the main form it only have menus, because i don't know how i can list the registry's on it, remember that i want to list only the names in the list and how i can add more registry's, not overwrite the existing one. Here is my code:
package contacts;
import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
public class simplecontacts extends MIDlet implements CommandListener, ItemStateListener {
private Display display;
private Alert alerta;
private Form telainicial, adicionaReg;
private TextField nome, telefone;
private Command sair, adicionar, voltar, salvar, zerar;
private RecordStore rs = null;
String tipo, mensagem;
boolean gravacaoOK = false;
private ChoiceGroup tipo_contato;
private String[] listaTipo = {"Amigos", "Família", "Trabalho"};
public simplecontacts() {
display = Display.getDisplay(this);
sair = new Command("Sair", Command.EXIT, 0);
adicionar = new Command("Adicionar Registros", Command.SCREEN, 1);
zerar = new Command("Zerar Registros", Command.SCREEN, 1);
voltar = new Command("Voltar", Command.SCREEN, 1);
salvar = new Command("Salvar Registro", Command.SCREEN, 1);
nome = new TextField("Nome:", "", 20, TextField.ANY);
telefone = new TextField("Telefone:", "", 10, TextField.PHONENUMBER);
telainicial = new Form("Simple Contacts");
telainicial.addCommand(sair);
telainicial.addCommand(adicionar);
telainicial.addCommand(zerar);
telainicial.setCommandListener(this);
adicionaReg = new Form("Adiciona Contato");
tipo_contato = new ChoiceGroup("Tipo do Contato", Choice.POPUP, listaTipo, null);
adicionaReg.append(nome);
adicionaReg.append(telefone);
adicionaReg.append(tipo_contato);
adicionaReg.addCommand(salvar);
adicionaReg.addCommand(voltar);
adicionaReg.setItemStateListener(this);
adicionaReg.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
notifyDestroyed();
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
try {
rs = RecordStore.openRecordStore("SimpleContacts", true);
} catch (Exception exc) {
mostrarAlerta("Erro ao abrir o RecordStore -", exc.toString());
}
display.setCurrent(telainicial);
}
private void mostrarAlerta(String tipoAlerta, String msg) {
alerta = new Alert(tipoAlerta, msg, null, AlertType.WARNING);
alerta.setTimeout(Alert.FOREVER);
display.setCurrent(alerta);
}
private void adicionarRegistro() {
try {
ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
DataOutputStream DOS = new DataOutputStream(BAOS);
DOS.writeUTF(nome.getString());
DOS.writeUTF(telefone.getString());
DOS.writeInt(tipo_contato.getSelectedIndex());
byte [] bRec = BAOS.toByteArray();
rs.addRecord(bRec, 0, bRec.length);
DOS.close();
BAOS.close();
display.setCurrent(telainicial);
} catch (Exception exc) {
mostrarAlerta("Erro ao adicionar -", exc.toString());
}
}
private void zerarRegistros(){
try {
rs.closeRecordStore();
RecordStore.deleteRecordStore("SimpleContacts");
rs = RecordStore.openRecordStore("SimpleContacts", true);
} catch (Exception exc) {
mostrarAlerta("Erro ao recriar o RecordStore -", exc.toString());
}
display.setCurrent(telainicial);
}
public void itemStateChanged (Item item) {
if (item instanceof TextField) {
String teste = ((TextField)item).getString();
if (teste != "") {
gravacaoOK = true;
}
}
}
public void commandAction(Command com, Displayable dis) {
if (com == sair) {
try {
rs.closeRecordStore();
} catch (Exception exc) {
mostrarAlerta("Erro fechando o RedcorStore -", exc.toString());
}
destroyApp(true);
} else if (com == adicionar) {
nome.setString("");
telefone.setString("");
display.setCurrent(adicionaReg);
} else if (com == salvar) {
if (gravacaoOK == false) {
mostrarAlerta("Registro vazio não será salvo", "Tente Novamente");
} else {
adicionarRegistro();
}
} else if (com == zerar) {
zerarRegistros();
} else if (com == voltar) {
display.setCurrent(telainicial);
}
}
}
Thanks,
Nathan Paulino Campos