刚学J2ME不到一星期,写不下去了,同学们帮我完善吧!(RMS操作范例)

发表于:2007-07-04来源:作者:点击数: 标签:
import java x.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.*; public class RMS MI Dlet extends MIDlet implements CommandListener { private Display display; private Ticker news; pr
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

public class RMSMIDlet extends MIDlet implements CommandListener {
    private Display display;
    private Ticker news;
    private Command exitCommand;
    private Command exeCommand;
    private Command saveCommand;
    private Command delCommand;
    private List menu;
    private String[] menuItem = {"通讯录清单","新增资料","删除资料","修改资料"};
    private List listAddress;
    private Form form;
    TextField nameField = new TextField("姓名","",20,TextField.ANY);
    TextField emailField = new TextField("电邮","",20,TextField.ANY);
    TextField telField = new TextField("电话","",20,TextField.NUMERIC);
    String dbname = new String("RMS");
    private String current = new String("");
    //结构化
    public RMSMIDlet() {
        news = new Ticker("号外:元旦只放一天!");
        exitCommand = new Command("离开",Command.EXIT,1);
        exeCommand = new Command("确定",Command.SCREEN,2);
        saveCommand = new Command("保存",Command.SCREEN,2);
        delCommand = new Command("删除",Command.SCREEN,2);
        display = Display.getDisplay(this);
    }
    //初始化
    public void startApp() {
        menu = new List(null,List.IMPLICIT,menuItem,null);
        menu.setTicker(news);
        menu.addCommand(exitCommand);
        menu.addCommand(exeCommand);
        menu.setCommandListener(this);
        current = "主菜单";
        display.setCurrent(menu);
    }
    //终止
    public void pauseApp() {}
    //销毁
    public void destroyApp(boolean unconditional) {}
    //按钮监听
    public void commandAction(Command c,Displayable s) {
        if(c == exitCommand && current.equals("主菜单")) {
            destroyApp(false);
            notifyDestroyed();
        }
        if(c == exitCommand && current.equals("通讯录清单")) {
            startApp();
        }
        if(c == exitCommand && current.equals("新增资料")) {
            startApp();
        }
        if(c == exitCommand && current.equals("删除资料")) {
            startApp();
        }
        if(c == exitCommand && current.equals("修改资料")) {
            startApp();
        }
        if(c == exitCommand && current.equals("详细资料")) {
            listAdress();
        }
        if(c == saveCommand) {
            addData();
            startApp();
        }
        if(c == delCommand) {
            deleteRS(dbname);
            startApp();
        }
        if(c == exeCommand && current.equals("通讯录清单")) {
            List tmp = (List) s ;
            searchData(tmp.getString(tmp.getSelectedIndex())) ;
        }
        if(c == exeCommand && current.equals("修改资料")) {
            //startApp();
        }
        if((c == List.SELECT_COMMAND && current.equals("主菜单"))||(c.getLabel().equals("确定"))) {
            List tmp = (List) s;
            switch(tmp.getSelectedIndex()) {
                case 0:
                    listAdress();
                    break;
                case 1:
                    addAdress();
                    break;
                case 2:
                    delAdress();
                    break;
                case 3:
                    modifyAdress();
                    break;
            }
        }
    }
    //显示清单
    public void listAdress() {
        listAddress = new List("通讯录清单",List.IMPLICIT);
        listAddress.addCommand(exitCommand);
        listAddress.addCommand(exeCommand);
        listAddress.setCommandListener(this);
        RecordStore rs = openRS(dbname) ;
        if(rs == null) {
            notifyDestroyed();
        }
        else {
            try {
                RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
                AddressData data = new AddressData() ;
                if(re.numRecords() == 0) {
                    listAddress.append("沒有任何数据",null) ;
                    current = "通讯录清单" ;
                    display.setCurrent(listAddress) ;
                    return;
                }
                while(re.hasNextElement()) {
                    byte[] tmp = re.nextRecord() ;
                    data.decode(tmp) ;
                    listAddress.append(data.name,null) ;
                }
                rs.closeRecordStore() ;
            }
            catch(Exception e) { }
        }
        current = "通讯录清单";
        display.setCurrent(listAddress);
    }
    //详细资料
    public void searchData(String name) {
        form = new Form("详细资料") ;
        form.addCommand(exitCommand) ;
        form.setCommandListener(this) ;
        RecordStore rs = openRS(dbname) ;
        if( rs == null ) {
            notifyDestroyed();
        }
        else {
            try {
                RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
                AddressData data = new AddressData() ;
                while(re.hasNextElement()) {
                    byte[] tmp = re.nextRecord() ;
                    data.decode(tmp) ;
                    if(data.name.equals(name)) {
                        form.append("姓名: \n") ;
                        form.append(data.name+"\n") ;
                        form.append("电邮: \n") ;
                        form.append(data.email+"\n") ;                        
                        form.append("电话: \n") ;
                        form.append(data.tel+"\n") ;
                        display.setCurrent(form) ;
                        return ;
                    }
                }
                rs.closeRecordStore() ;
            }
            catch(Exception e) { }
        }
    }
    //新增资料界面
    public void addAdress() {
        form = new Form("新增资料");
        form.append(nameField);
        form.append(emailField);
        form.append(telField);
        form.addCommand(exitCommand);
        form.addCommand(saveCommand);
        form.setCommandListener(this);
        current = "新增资料";
        display.setCurrent(form);
    }
    //新增资料
    public void addData() {
        AddressData data = new AddressData();
        data.name = nameField.getString();
        data.email = emailField.getString() ;
        data.tel = telField.getString() ;
        byte[] tmp = data.encode() ;
        RecordStore rs = openRS(dbname) ;
        if( rs == null ) {
            notifyDestroyed();
        }
        else {
            try {
                rs.addRecord(tmp,0,tmp.length) ;
                rs.closeRecordStore() ;
            }
            catch(Exception e) { }
        }
    }
    //删除资料界面
    public void delAdress() {
        form = new Form("删除资料");
        form.append("你确定要删除全部资料吗?");
        form.addCommand(exitCommand);
        form.addCommand(delCommand);
        form.setCommandListener(this);
        current = "删除资料";
        display.setCurrent(form);
    }
    //修改资料界面
    public void modifyAdress() {
        listAddress = new List("修改资料",List.IMPLICIT);
        listAddress.addCommand(exitCommand);
        listAddress.addCommand(exeCommand);
        listAddress.setCommandListener(this);
        RecordStore rs = openRS(dbname) ;
        if(rs == null) {
            notifyDestroyed();
        }
        else {
            try {
                RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
                AddressData data = new AddressData() ;
                if(re.numRecords() == 0) {
                    listAddress.append("沒有任何数据",null) ;
                    current = "修改资料" ;
                    display.setCurrent(listAddress) ;
                    return;
                }
                while(re.hasNextElement()) {
                    byte[] tmp = re.nextRecord() ;
                    data.decode(tmp) ;
                    listAddress.append(data.name,null) ;
                }
                rs.closeRecordStore() ;
            }
            catch(Exception e) { }
        }
        current = "修改资料";
        display.setCurrent(listAddress);
    }
    //打开或创建数据库
    public RecordStore openRS(String dbTable) {
        RecordStore rs = null ;
        if(dbTable.length() > 32)
            return null ;
        try {
            rs = RecordStore.openRecordStore(dbTable,true) ;
            return rs ;
        }
        catch(Exception e) {
            return null ;
        }
    }
    //删除数据库
    public boolean deleteRS(String dbTable) {
        if(dbTable.length() > 32)
            return false ;
        try {
            RecordStore.deleteRecordStore(dbTable) ;
        }
        catch(Exception e) {
            return false ;
        }
        return true ;
    }
}








class AddressData {
    String name;
    String email;
    String tel;
    public AddressData() {
        name = "NO NAME";
        email = "NO EMAIL";
        tel = "NO TEL";
    }
    public byte[] encode() {
        byte[] result = null;
        try {
            ByteArrayOutputStream bos = new    ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream (bos);
            dos.writeUTF(name);
            dos.writeUTF(email);
            dos.writeUTF(tel);
            result = bos.toByteArray();
            dos.close();
            bos.close();
        }
        catch(Exception e) { }
        return result;
    }
    public void decode(byte[] data) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream (bis);
            name = dis.readUTF();
            email = dis.readUTF();
            tel = dis.readUTF();
            dis.close();
            bis.close();
        }
        catch(Exception e) { }
    }

原文转自:http://www.ltesting.net