📄 rmstest.java
字号:
/*
* RMSTest.java
*
* Copyright 2001 SkyArts. All Rights Reserved.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.rms.*;
/**
* Record Management System(RMS)记录的添加、删除、改变的范例的MIDlet
*
* @author Hideki Yonekawa
* @version 1.0
*/
public class RMSTest extends MIDlet implements CommandListener {
/** 显示记录一览的List变量 */
private List recordList;
/** 保持记录的ID的Vector。与记录的一览List相同顺序 */
private Vector idVect = new Vector();
/** 删除命令 */
private Command delCmd = new Command("删除", Command.SCREEN, 1);
/** 添加命令 */
private Command addCmd = new Command("添加", Command.SCREEN, 5);
/** 储存RecordStore名称的常量 */
private static final String RS_NAME = "RMSTest";
/** 储存用以取得时间的Calendar对象的常量 */
private Calendar calendar;
/** 构造函数 */
public RMSTest() {
//取得Calendar对象
calendar = Calendar.getInstance();
//制作记录的一览表List
createRecordList();
//显示记录的List
Display.getDisplay(this).setCurrent(recordList);
}
/** MIDlet开始时调用的方法 */
protected void startApp() throws MIDletStateChangeException {}
/** MIDlet暂停时调用的方法 */
protected void pauseApp() {}
/** MIDlet结束时调用的方法 */
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {}
/** 制作记录一览表List的方法 */
private void createRecordList() {
//建立记录List
recordList = new List("记录一览表", Choice.IMPLICIT);
//附加记录
recordList.addCommand(delCmd);
recordList.addCommand(addCmd);
recordList.setCommandListener(this);
RecordStore rs = null;
try {
//清除储存ID的Vector
idVect.removeAllElements();
//打开RecordStore
rs = RecordStore.openRecordStore(RS_NAME, true);
//取得记录的Enumeration
RecordEnumeration recordEnumeration = rs.enumerateRecords(null, null, true);
while(recordEnumeration.hasNextElement()) {
//取得Enumeration中的记录ID
int id = recordEnumeration.nextRecordId();
//以ID取得byte数组写成String
String tmpSt = new String(rs.getRecord(id));
//在储存ID的Vector添加ID
idVect.addElement(new Integer(id));
//在记录的List中增加字符串
recordList.append(tmpSt, null);
}
}catch(RecordStoreException e) {
}finally {
if(rs != null) {
try {
//关闭RecordStore
rs.closeRecordStore();
}catch(RecordStoreException ex) {}
}
}
}
/**
* Command的事件发生时
* 选择List时调用的方法
*/
public void commandAction(Command c, Displayable d) {
if(d == recordList) { //显示记录的List时
if(c == List.SELECT_COMMAND) { //选择List时
if(recordList.getSelectedIndex() < 0) return;
//改变被选择的记录
doSave(recordList.getSelectedIndex(), getTimeSt());
createRecordList();
}else { //List没被选择时
if(c == delCmd) { //选择删除命令时
if(recordList.getSelectedIndex() < 0) return;
//从选择的List的Index将记录删除
doDelete(recordList.getSelectedIndex());
}else if(c == addCmd) { //选择添加命令时
doSave(-1, getTimeSt());
createRecordList();
}
}
//显示记录的List
Display.getDisplay(this).setCurrent(recordList);
}
}
/** 以指定的Index与文字来添加、更新、删除命令的方法 */
private boolean doSave(int inx, String writeSt) {
//更新处理
RecordStore rs = null;
try {
//打开RecordStore
rs = RecordStore.openRecordStore(RS_NAME, true);
if(inx < 0) { //制作新记录时
//确认可储存的容量大小
int availableSize = rs.getSizeAvailable();
byte[] writeBytes = writeSt.getBytes();
if(availableSize >= writeBytes.length) { //未超过容量大小时
//为了重做List而将ID写到Vector
//添加记录。在添加时传回的ID先写在储存ID的Vector中
int id = rs.addRecord(writeBytes, 0, writeBytes.length);
idVect.addElement(new Integer(id));
}else { //超过容量大小时
return false;
}
}else { //变更时
//从储存ID的Vector取得该ID
int id = ((Integer)idVect.elementAt(inx)).intValue();
if(writeSt.length() > 0) { //有文字时
//更新记录
byte[] writeBytes = writeSt.getBytes();
rs.setRecord(id, writeBytes, 0, writeBytes.length);
}else { //没有文字时
//删除记录
rs.deleteRecord(id);
}
}
}catch(RecordStoreException e) {
}finally {
if(rs != null) {
try {
//关闭RecordStore
rs.closeRecordStore();
}catch(RecordStoreException ex) {}
}
}
return true;
}
/** 将指定Index的记录删除的方法 */
private void doDelete(int inx) {
//调用doSave方法,将记录删除
doSave(inx, "");
recordList.delete(inx);
idVect.removeElementAt(inx);
}
/** 传回表示现在时间的字符串 */
private String getTimeSt() {
calendar.setTime(new Date());
StringBuffer stBuffer = new StringBuffer();
stBuffer.append(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) +":"); //时
stBuffer.append(String.valueOf(calendar.get(Calendar.MINUTE)) +" "); //分
stBuffer.append(String.valueOf(calendar.get(Calendar.SECOND))); //秒
return stBuffer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -