课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——基于内存记录管理的通讯录管理程序


  共有15204人关注过本帖树形打印复制链接

主题:程序代码——基于内存记录管理的通讯录管理程序

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——基于内存记录管理的通讯录管理程序  发帖心情 Post By:2008/10/9 20:30:50 [只看该作者]

import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        Display display;
        MainForm mainForm;

        public Exec() {
                mainForm = new MainForm("通讯录", this);
                display = Display.getDisplay(this);
        }

        public void startApp() {
                display.setCurrent(mainForm);
        }

        public void pauseApp() {
                display.setCurrent(null);
        }

        public void destroyApp(boolean unconditional) {
        }

        public Display getDisplay() {
                return display;
        }

        public void exitApp() {
                destroyApp(true);
                notifyDestroyed();
        }
}

class MainForm extends Form implements CommandListener {
        Command cmd1 = new Command("退出", Command.BACK, 1);
        Command cmd2 = new Command("显示", Command.OK, 1);
        Exec parent;
        RecordList recordList;

        public MainForm(String title, Exec parent) {
                super(title);
                this.parent = parent;
                recordList = new RecordList("主界面", parent);
                addCommand(cmd1);
                addCommand(cmd2);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmd1) {
                        parent.exitApp();
                } else if (c == cmd2) {
                        parent.getDisplay().setCurrent(recordList);
                }
        }
}

class RecordList extends List implements CommandListener {
        Command cmdAppend = new Command("追加于后", Command.OK, 1);
        Command cmdInsert = new Command("插入于前", Command.OK, 1);
        Command cmdSelect = new Command("查看", Command.BACK, 1);
        Exec parent;
        NewTextBox newTextBox;
        BrowseTextBox browseTextBox;
        InfoAlert infoAlert = new InfoAlert();
        MemoryDataManager dm = MemoryDataManager.getDataManager();

        public RecordList(String title, Exec parent) {
                super(title, Choice.IMPLICIT);
                this.parent = parent;
                newTextBox = new NewTextBox("信息", 100, parent, this);
                browseTextBox = new BrowseTextBox("内容", 100, parent, this);
                addCommand(cmdAppend);
                addCommand(cmdInsert);
                addCommand(cmdSelect);
                setCommandListener(this);
                fillRecords();
        }

        public void fillRecords() {
                Hashtable records = dm.getRecords();
                Enumeration enu = records.keys();
                while (enu.hasMoreElements()) {
                        String key = (String) enu.nextElement();
                        this.insert(0, key, null);
                }
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmdAppend || c == cmdInsert) {
                        newTextBox.clearAll();
                        if (c == cmdAppend)
                                newTextBox.setFlag("after");
                        else
                                newTextBox.setFlag("before");
                        parent.getDisplay().setCurrent(newTextBox);
                } else if (c == cmdSelect) {
                        if (this.size() > 0) {
                                parent.getDisplay().setCurrent(browseTextBox);
                                browseTextBox.getRecord();
                        } else
                                parent.getDisplay().setCurrent(infoAlert);
                }

                // 测试代码:显示存储的记录内容
                // Hashtable records = dm.getRecords();
                // Enumeration enu = records.keys();
                // while (enu.hasMoreElements()) {
                // String key = (String) enu.nextElement();
                // Hashtable hs = (Hashtable) records.get(key);
                // System.out.println(key + ":" + hs);
                // }
        }

        public MemoryDataManager getDm() {
                return dm;
        }
}

class NewTextBox extends Form implements CommandListener {
        Command cmdOK = new Command("确认", Command.OK, 1);
        Exec parent;
        RecordList list;
        String flag;
        TextField tfName = new TextField("姓名:", "", 8, TextField.ANY);
        TextField tfEmail = new TextField("Email", "", 20, TextField.EMAILADDR);
        TextField tfPhone = new TextField("电话:", "", 20, TextField.PHONENUMBER);

        public NewTextBox(String title, int size, Exec parent, RecordList list) {
                super(title);
                this.parent = parent;
                this.list = list;

                append(tfName);
                append(tfEmail);
                append(tfPhone);

                addCommand(cmdOK);
                setCommandListener(this);
        }

        public void commandAction(Command c, Displayable arg1) {
                MemoryDataManager dm = list.getDm();
                Hashtable newOne = new Hashtable();
                newOne.put("name", tfName.getString());
                newOne.put("Email", tfEmail.getString());
                newOne.put("Phone", tfPhone.getString());
                int recordID = dm.addRecord(newOne);

                int index = list.getSelectedIndex();
                String result = String.valueOf(recordID);
                if (index < 0) {
                        list.append(result, null);
                } else {
                        if (flag.equals("after"))
                                list.insert(index + 1, result, null);
                        else {
                                list.insert(index, result, null);
                        }
                }
                parent.getDisplay().setCurrent(list);
        }

        public void setFlag(String flag) {
                this.flag = flag;
        }

        public void clearAll() {
                for (int i = 0; i < this.size(); i++) {
                        TextField tf = (TextField) this.get(i);
                        tf.setString("");
                }
        }
}

class BrowseTextBox extends Form implements CommandListener {
        Command cmdOK = new Command("确认", Command.OK, 1);
        Command cmdDelete = new Command("删除", Command.BACK, 1);
        Exec parent;
        RecordList list;
        TextField tfID = new TextField("ID号:", "", 8, TextField.ANY);
        TextField tfName = new TextField("姓名:", "", 8, TextField.ANY);
        TextField tfEmail = new TextField("Email", "", 20, TextField.EMAILADDR);
        TextField tfPhone = new TextField("电话:", "", 20, TextField.PHONENUMBER);

        public BrowseTextBox(String title, int size, Exec parent, RecordList list) {
                super(title);
                this.parent = parent;
                this.list = list;

                append(tfID);
                append(tfName);
                append(tfEmail);
                append(tfPhone);

                addCommand(cmdOK);
                addCommand(cmdDelete);
                setCommandListener(this);
        }

        public void getRecord() {
                String key = list.getString(list.getSelectedIndex());
                MemoryDataManager dm = list.getDm();
                Hashtable records = dm.getRecords();
                Hashtable record = (Hashtable) records.get(key);
                tfID.setString((String) (record.get("id")));
                tfName.setString((String) (record.get("name")));
                tfEmail.setString((String) (record.get("Email")));
                tfPhone.setString((String) (record.get("Phone")));
        }

        public void deleteRecord() {
                String key = list.getString(list.getSelectedIndex());
                MemoryDataManager dm = list.getDm();
                Hashtable records = dm.getRecords();
                records.remove(key);
        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmdOK) {
                        parent.getDisplay().setCurrent(list);
                } else if (c == cmdDelete) {
                        int index = list.getSelectedIndex();
                        deleteRecord();
                        list.delete(index);
                        parent.getDisplay().setCurrent(list);
                }
        }
}

class InfoAlert extends Alert {
        public InfoAlert() {
                super("通知");
                setType(AlertType.INFO);
                setTimeout(1000);
                setString("没有记录");
        }
}

class MemoryDataManager {
        Hashtable records = new Hashtable();

        static int totalNumber = 1;

        public MemoryDataManager() {
                Hashtable h1 = new Hashtable();
                h1.put("name", "李树青");
                h1.put("Email", "leeshuqing@163.com");
                h1.put("Phone", "00862584028398");
                addRecord(h1);

                Hashtable h2 = new Hashtable();
                h2.put("name", "胡哥");
                h2.put("Email", "huge@sina.com");
                h2.put("Phone", "00861000001111");
                addRecord(h2);
        }

        public static MemoryDataManager getDataManager() {
                return new MemoryDataManager();
        }

        public int addRecord(Hashtable record) {
                record.put("id", String.valueOf(totalNumber));
                records.put(String.valueOf(totalNumber), record);
                return totalNumber++;
        }

        public void deleteRecord(Hashtable record) {
                records.remove(record.get("id"));
        }

        public Hashtable getRecords() {
                return records;
        }

}

[此贴子已经被作者于2010-12-12 18:36:48编辑过]

 回到顶部