public
Created

Fully Featured RecordStore Example(Java ME)

  • Download Gist
midp.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
package contacts;
import java.io.*;
import java.util.Vector;
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 {
private Display display;
private Alert alerta;
private Form initialScreen, addReg;
private TextField name, telefone;
private Command exit, add, back, save, clear;
private RecordStore rs = null;
String type, message;
boolean recordOK = false;
private ChoiceGroup contact_type;
private String[] listType = {"Amigos", "Família", "Trabalho", "Teste"};
private Form listReg;
private Command list, delete, ord, filter;
private ChoiceGroup listRegistrys;
boolean deletedFlags[] = null;
private Vector vecNames;
public simplecontacts() {
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 0);
add = new Command("Add Registrys", Command.SCREEN, 1);
clear = new Command("Clear Registrys", Command.SCREEN, 1);
back = new Command("Back", Command.SCREEN, 1);
save = new Command("Save Registry", Command.SCREEN, 1);
name = new TextField("Name:", "", 20, TextField.ANY);
telefone = new TextField("Telefone:", "", 10, TextField.PHONENUMBER);
list = new Command("List Registrys", Command.SCREEN, 1);
listRegistrys = new ChoiceGroup("Registrys", ChoiceGroup.MULTIPLE);
vecNames = new Vector();
delete = new Command("Delete Registrys", Command.SCREEN, 1);
ord = new Command("Order Registrys", Command.SCREEN, 1);
filter = new Command("Filter Registrys", Command.SCREEN, 1);
initialScreen = new Form("Simple Contacts");
initialScreen.addCommand(exit);
initialScreen.addCommand(add);
initialScreen.addCommand(clear);
initialScreen.addCommand(list);
initialScreen.addCommand(ord);
initialScreen.addCommand(filter);
initialScreen.addCommand(clear);
initialScreen.setCommandListener(this);
addReg = new Form("Add Contact");
contact_type = new ChoiceGroup("Types", Choice.POPUP, listType, null);
addReg.append(name);
addReg.append(telefone);
addReg.append(contact_type);
addReg.addCommand(save);
addReg.addCommand(back);
addReg.setCommandListener(this);
listReg = new Form("");
listReg.append(listRegistrys);
listReg.addCommand(delete);
listReg.addCommand(back);
listReg.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
try {
rs = RecordStore.openRecordStore("SContacts", true);
} catch (Exception exc) {
mostrarAlerta("Erro ao abrir o RecordStore -", exc.toString());
}
display.setCurrent(initialScreen);
}
private void gerarTela(Vector nomes, int totalElementos) {
for (int i = listRegistrys.size(); i > 0; i--)
listRegistrys.delete(i - 1);
for (int i = 0; i < nomes.size(); i++) {
Nomes item = (Nomes) vecNames.elementAt(i);
String nome = item.getNome();
String telefone = item.getTelefone();
listRegistrys.append(nome + " " + telefone, null);
}
display.setCurrent(listReg);
}
private void listarRegistros(FiltroNome f, ComparadorNome c) {
RecordEnumeration rEnum = null;
vecNames.removeAllElements();
try {
rEnum = rs.enumerateRecords(f, c, false);
int posicao = 0;
while (rEnum.hasNextElement()) {
int id = rEnum.nextRecordId();
byte[] dados = rs.getRecord(id);
ByteArrayInputStream BAIS = new ByteArrayInputStream(dados);
DataInputStream DIS = new DataInputStream(BAIS);
String nomeLido = DIS.readUTF();
String telefoneLido = DIS.readUTF();
int tipo = DIS.readInt();
Nomes itemAgenda = new Nomes(nomeLido, telefoneLido, tipo, id, posicao);
vecNames.addElement(itemAgenda);
posicao++;
}
gerarTela(vecNames, rEnum.numRecords());
} catch (Exception exc) {
mostrarAlerta("Erro na listagem -", exc.toString());
} finally {
rEnum.destroy();
}
}
private void apagarRegistros() {
try {
for (int i = 0; i < deletedFlags.length; i++) {
if (deletedFlags[i] == true) {
Nomes itemAgenda = (Nomes) vecNames.elementAt(i);
try {
rs.deleteRecord(itemAgenda.getRecordId());
} catch (Exception exc) {
}
}
}
display.setCurrent(initialScreen);
} catch (Exception exc) {
Alert erro = new Alert("Nenhum registro foi selecionado -",
"Refaça a operação ou volte para a tela inicial", null, AlertType.ERROR);
erro.setTimeout(Alert.FOREVER);
display.setCurrent(erro);
}
}
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(name.getString());
DOS.writeUTF(telefone.getString());
DOS.writeInt(contact_type.getSelectedIndex());
byte [] bRec = BAOS.toByteArray();
rs.addRecord(bRec, 0, bRec.length);
DOS.close();
BAOS.close();
display.setCurrent(initialScreen);
} catch (Exception exc) {
mostrarAlerta("Erro ao adicionar -", exc.toString());
}
}
private void zerarRegistros(){
try {
rs.closeRecordStore();
RecordStore.deleteRecordStore("SContacts");
rs = RecordStore.openRecordStore("SContacts", true);
} catch (Exception exc) {
mostrarAlerta("Erro ao recriar o RecordStore -", exc.toString());
}
display.setCurrent(initialScreen);
}
public void commandAction(Command com, Displayable dis) {
if (com == exit) {
try {
rs.closeRecordStore();
} catch (Exception exc) {
mostrarAlerta("Erro fechando o RecordStore -", exc.toString());
}
destroyApp(true);
} else if (com == add) {
name.setString("");
telefone.setString("");
display.setCurrent(addReg);
} else if (com == save) {
adicionarRegistro();
} else if (com == clear) {
zerarRegistros();
} else if (com == back) {
display.setCurrent(initialScreen);
} else if (com == delete) {
apagarRegistros();
} else if (com == list) {
listReg.setTitle("Listing All");
listRegistrys.deleteAll();
listarRegistros(null, null);
} else if (com == ord) {
listReg.setTitle("Registrys Ordened");
listRegistrys.deleteAll();
ComparadorNome comparador = new ComparadorNome();
listarRegistros(null, comparador);
} else if (com == filter) {
listReg.setTitle("Filtred Registrys");
listRegistrys.deleteAll();
FiltroNome filtro = new FiltroNome("Thiago");
ComparadorNome comparador = new ComparadorNome();
listarRegistros(filtro, comparador);
}
}
}
class FiltroNome implements RecordFilter {
String nome;
public FiltroNome(String nomeFiltrado) {
nome = nomeFiltrado;
}
public boolean matches(byte[] registro) {
try {
ByteArrayInputStream BAIS = new ByteArrayInputStream(registro);
DataInputStream DIS = new DataInputStream(BAIS);
String nomeLido = DIS.readUTF();
DIS.close();
BAIS.close();
if (nome.compareTo(nomeLido) == 0)
return true;
} catch (Exception exc) {
System.out.println("Exceção: " + exc.getMessage());
}
return false;
}
}
class ComparadorNome implements RecordComparator {
public int compare(byte[] reg1, byte[] reg2){
try {
ByteArrayInputStream BAIS = new ByteArrayInputStream(reg1);
DataInputStream DIS = new DataInputStream(BAIS);
String nome1 = DIS.readUTF();
DIS.close();
BAIS.close();
ByteArrayInputStream BAIS2 = new ByteArrayInputStream(reg2);
DataInputStream DIS2 = new DataInputStream(BAIS2);
String nome2 = DIS2.readUTF();
DIS2.close();
BAIS2.close();
if (nome1.compareTo(nome2) < 0) {
return PRECEDES;
} if (nome1.compareTo(nome2) > 0) {
return FOLLOWS;
}
} catch (Exception exc) {
System.out.println("Exceção: " + exc.getMessage());
}
return EQUIVALENT;
}
}
class Nomes {
private String nome, telefone;
private int recordId;
public Nomes(String nome, String telefone, int tipo, int recordId, int posicao) {
this.nome = nome;
this.telefone = telefone;
this.recordId = recordId;
}
public String getNome() {
return nome;
}
public String getTelefone() {
return telefone;
}
public void setNome(String nome) {
this.nome = nome;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public int getRecordId() {
return recordId;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.