使用RMS在JaveME中的应用

时间:2014-03-31 14:09:49

标签: java-me rms

我正在尝试使用RMS在Jave ME中编写应用程序。应用程序存储有关快递公司客户的信息。

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;

public class ClientsApp extends MIDlet implements CommandListener {
// controls
private Display screen = null; 
private List menu = null;
private Form addClientForm = null;
private Form showClientForm = null;
private RecordStore clients;
private ByteArrayOutputStream stream = null;
private DataOutputStream out = null;
private byte[] dates;

TextField name = null;
TextField surname = null;
TextField email = null;
TextField phone = null;
DateField date = null;
TextField price = null;
TextField description = null;


// comands
private final Command backCommand;
private final Command mainMenuCommand;
private final Command exitCommand; 
private final Command addClientCommand;

public ClientsApp() {
    // initializating controls and comands
    menu = new List("Lista klientów", Choice.IMPLICIT);
    backCommand = new Command("Cofnij", Command.BACK, 0);
    mainMenuCommand = new Command("Main", Command.SCREEN, 1);
    exitCommand = new Command("Koniec", Command.EXIT, 2);
    addClientCommand = new Command("Zapisz", Command.OK, 3);    
    stream = new ByteArrayOutputStream();
    out = new DataOutputStream(stream);

    menu.append("Dodaj klienta", null);
    menu.append("Przegladaj klientow", null);
    menu.append("Usun klienta", null);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

protected void startApp() throws MIDletStateChangeException {
    screen = Display.getDisplay(this);
    screen.setCurrent(menu);

    try {
        clients = RecordStore.openRecordStore("clients", false, RecordStore.AUTHMODE_PRIVATE, false);
    }
    catch(RecordStoreException exc) {
    }

    menu.addCommand(exitCommand);
    menu.setCommandListener(this);
}

public void commandAction(Command cmd, Displayable dsp) {
    if(cmd.getCommandType() == Command.EXIT) {
        try{
            destroyApp(false);
            notifyDestroyed();
        }
        catch(Exception exc) {
            exc.printStackTrace();              
        }
    }
    else if(cmd.getCommandType() == Command.BACK) {
        screen.setCurrent(menu);
    }
    else if(cmd.getCommandType() == Command.OK) {
        try {
            out.writeUTF(name.getString());
            out.writeUTF(surname.getString());
            out.writeUTF(email.getString());
            out.writeUTF(phone.getString());
            out.writeUTF(date.getDate().toString());
            out.writeUTF(price.getString());
            out.writeUTF(description.getString());
            dates = stream.toByteArray();

            clients.addRecord(dates, 0, dates.length);

            stream.close();
            out.close();
            clients.closeRecordStore();
        }
        catch(Exception exc) {

        }
    }
    else {
        List option = (List) screen.getCurrent();

        switch(option.getSelectedIndex()) {
        case 0 : {
            addClients();
            break;
        }
        case 1 : {
            showClients();
            break;
        }
        case 2 : {
            deleteClients();
            break;
        }
        }
    }
}

protected void addClients() {
    addClientForm = new Form("Dodaj klienta");
    name = new TextField("Imię klienta", "", 10, TextField.ANY);
    surname = new TextField("Nazwisko klienta", "", 15, TextField.ANY);
    email = new TextField("Email klienta", "", 20, TextField.EMAILADDR);
    phone = new TextField("Numer telefonu", "", 12, TextField.PHONENUMBER);
    date = new DateField("Data dostarczenia", DateField.DATE);
    price = new TextField("Do zapłaty", "", 6, TextField.NUMERIC);
    description = new TextField("Uwagi", "", 50, TextField.ANY);

    addClientForm.append(name);
    addClientForm.append(surname);
    addClientForm.append(email);
    addClientForm.append(phone);
    addClientForm.append(date);
    addClientForm.append(price);
    addClientForm.append(description);
    screen.setCurrent(addClientForm);

    addClientForm.addCommand(backCommand);
    addClientForm.addCommand(addClientCommand);
    addClientForm.setCommandListener(this);
}

protected void showClients() {
    TextBox info = new TextBox("Klienci", null, 100, 0);

    RecordEnumeration iterator = null;
    String str = null;
    byte[] temp = null;

    try {
        iterator = clients.enumerateRecords(null, null, false);

        while(iterator.hasNextElement()) {
            temp = iterator.nextRecord();
        }

        for(int i = 0; i < temp.length; i++) {
            str += (char) temp[i];
        }
        System.out.println(str);
        clients.closeRecordStore();
    }
    catch(Exception exc) {

    }       

    info.setString(str);
    screen.setCurrent(info);
}
}

从RecordStore写入/读取信息不起作用。我没有任何例外。有人能帮助我吗?

PS抱歉我的语言不好。

1 个答案:

答案 0 :(得分:0)

你确定没有任何例外吗?捕捉块是空的...... 我看到几个问题: 你不应该在createIfNecessary(第二个参数)设置为true的情况下打开记录存储吗? 在ShowClients方法中,您应该使用DataInputStream从记录中读取项目(字节数组'temp'),temp over temp很奇怪。并且当商店为空时检查空'temp'以避免NPE也会丢失。 在OK命令和ShowClients中,商店已关闭,因此下次它将因RecordStoreNotOpenException而失败。

我还会考虑在调用stream.toByteArray()之前刷新'out'流,虽然在这种情况下(DataOutputStrea / ByteArrayOutputStream)它只不过是一个好习惯..