?? 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 + -