import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
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);
}
}
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();
Person person = new Person(tfName.getString(), tfEmail.getString(),
tfPhone.getString());
int recordID = dm.addRecord(person);
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();
Person person = (Person) records.get(key);
tfID.setString(String.valueOf((person.getID())));
tfName.setString(person.getName());
tfEmail.setString(person.getEmail());
tfPhone.setString(person.getPhone());
}
public void deleteRecord() {
String key = list.getString(list.getSelectedIndex());
MemoryDataManager dm = list.getDm();
dm.deleteRecord(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 {
RecordStore rs;
public MemoryDataManager() {
try {
rs = RecordStore.openRecordStore("PersonInfo", true);
if (rs.getNumRecords() == 0) {
Person person1 = new Person("李树青", "leeshuqing@163.com",
"00862584028398");
addRecord(person1);
Person person2 = new Person("胡哥", "huge@sina.com",
"00861000001111");
addRecord(person2);
rs.closeRecordStore();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static MemoryDataManager getDataManager() {
return new MemoryDataManager();
}
public int addRecord(Person person) {
int personID = 0;
try {
rs = RecordStore.openRecordStore("PersonInfo", true);
personID = rs.getNextRecordID();
person.setID(personID);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] data = person.serialize();
rs.addRecord(data, 0, data.length);
dos.close();
baos.close();
rs.closeRecordStore();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return personID;
}
public void deleteRecord(String key) {
try {
rs = RecordStore.openRecordStore("PersonInfo", true);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
byte[] tmp = re.nextRecord();
Person person = Person.deserialize(tmp);
int personID = person.getID();
if (personID == Integer.parseInt(key)) {
rs.deleteRecord(personID);
break;
}
}
rs.closeRecordStore();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public Hashtable getRecords() {
Hashtable results = new Hashtable();
try {
rs = RecordStore.openRecordStore("PersonInfo", true);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
byte[] tmp = re.nextRecord();
Person person = Person.deserialize(tmp);
int personID = person.getID();
results.put(String.valueOf(personID), person);
}
rs.closeRecordStore();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return results;
}
}
class Person {
int ID;
String name;
String Email;
String phone;
public Person() {
}
public Person(String name, String Email, String phone) {
setName(name);
setEmail(Email);
setPhone(phone);
}
public void setID(int id) {
ID = id;
}
public int getID() {
return ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(ID);
dos.writeUTF(name);
dos.writeUTF(Email);
dos.writeUTF(phone);
baos.close();
dos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return baos.toByteArray();
}
public static Person deserialize(byte[] data) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Person person = new Person();
try {
person.ID = dis.readInt();
person.name = dis.readUTF();
person.Email = dis.readUTF();
person.phone = dis.readUTF();
bais.close();
dis.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return person;
}
public String toString() {
return ID + ":" + name + ":" + Email + ":" + phone;
}
}
[此贴子已经被作者于2010-12-12 18:39:18编辑过]