📄 mobilememo.java
字号:
import java.util.*;
import java.lang.System;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.*;
public class MobileMemo extends MIDlet implements CommandListener {
private final String RS_NAME = "memos";
// Screens
private Display display;
private Form inputForm;
private List list;
private TextBox memoText;
// Command group
private Command add = new Command("添加", Command.OK, 1);
private Command show = new Command("显示", Command.OK, 1);
private Command ok = new Command("保存", Command.OK, 1);
private Command delete = new Command("删除", Command.OK, 1);
// Memos
private Memo currentMemo;
private Vector memos;
/**
* Constructor
*/
public MobileMemo() {
display = Display.getDisplay(this);
memos = new Vector();
// Generates the memo list
initilizeList();
// Generates the Memo screen
initilizeMemoText();
}
/**
// 启动MIDlet
*/
protected void startApp() throws MIDletStateChangeException {
// 从记录存储中读取信息
reloadFromRSM();
// 转到日记列表画面
display.setCurrent(list);
}
/**
* 从记录存储中更新信息
*/
public void reloadFromRSM() {
memos.removeAllElements();
// Gets records
RecordStore rs = null;
RecordEnumeration re = null;
try {
rs = RecordStore.openRecordStore(RS_NAME, true);
//列举记录
re=rs.enumerateRecords(null, null, true);
//re=rs.enumerateRecords(null,new TestComparator(), true);
//取得记录
while (re.hasNextElement()) {
int id = re.nextRecordId();// Gets the record ID
byte[] data = rs.getRecord(id);// Gets records
Memo memo = new Memo(data);// Generates memos
memo.setRecId(id);// * Sets the record ID
memos.addElement(memo);// Adds memos
}
} catch (Exception e) {
} finally {
// Destroys the RecordEnumeration
if (re != null) {
try {
re.destroy();
} catch (Exception e) {
}
}
// Closes the record store
if (rs != null) {
try {
rs.closeRecordStore();
} catch (Exception e) {
}
}
}
// 更新日记列表
initilizeList();
for (int i = 0; i < memos.size(); i++) {
Memo memo = (Memo) memos.elementAt(i);
//取出日记的date,利用Calendar转换成年月日,并添加到list中。
Calendar Now = Calendar.getInstance();
Now.setTime(memo.getDate());
String year = String.valueOf(Now.get(java.util.Calendar.YEAR));
String month = String.valueOf(Now.get(java.util.Calendar.MONTH) + 1);
String day = String.valueOf(Now.get(java.util.Calendar.DATE));
list.append(year+"年"+month+"月"+day+"日", null);
}
}
/*
* 初始化日记列表
*/
private void initilizeList() {
list = new List("日记列表", List.EXCLUSIVE);
list.addCommand(show);
list.addCommand(add);
list.setCommandListener(this);
}
/**
* 初始化日记内容
*/
private void initilizeMemoText() {
memoText = new TextBox("日记", "", 100, TextField.ANY);
memoText.addCommand(ok);
memoText.addCommand(delete);
memoText.setCommandListener(this);
}
protected void pauseApp() {}
protected void destroyApp(boolean bool) throws MIDletStateChangeException {}
public void commandAction(Command c, Displayable d) {
if (d == list) {
if (c == add) {
// Creates a new memo
currentMemo = createNewMemo();
} else if (c == show) {
// Gets a memo
int index = list.getSelectedIndex();
if(index == -1)return;
currentMemo = (Memo) memos.elementAt(index);
}
// Moves to the Memo screen
memoText.setString(currentMemo.getContent());
display.setCurrent(memoText);
} else if (d == memoText) {
if (c == ok) {
// 保存日记内容
currentMemo.setContent(memoText.getString());
writeMemo(currentMemo);
} else if (c == delete) {
//删除日记
deleteMemo(currentMemo);
}
// 转到日记列表画面
reloadFromRSM();
display.setCurrent(list);
}
}
/**
* 添加新日记
*/
public Memo createNewMemo() {
Memo memo = new Memo();
return changeMemoToRMS(memo, add);
}
/**
*保存日记
*/
public void writeMemo(Memo memo) {
changeMemoToRMS(memo, ok);
}
/**
* 删除日记
*/
public void deleteMemo(Memo memo) {
changeMemoToRMS(memo, delete);
}
/**
* 更新日记存储
*/
private Memo changeMemoToRMS(Memo memo, Command c) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(RS_NAME, true);
// 更新记录存储
if (c == ok) { // 编辑状态时的保存
int id = memo.getRecId();
rs.setRecord(id, memo.toBytes(), 0, memo.toBytes().length);
} else if (c == delete) { //删除日记
int id = memo.getRecId();
rs.deleteRecord(id);
} else if (c == add) { //添加新日记
int id = rs.addRecord(memo.toBytes(), 0, memo.toBytes().length);
memo.setRecId(id);
}
} catch (Exception e) {
} finally {
if (rs != null) {
try {
rs.closeRecordStore();
return memo;
} catch (Exception e) {
}
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -