enumerationtest.java

来自「java手机程序开发随书光盘源代码」· Java 代码 · 共 143 行

JAVA
143
字号
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class EnumerationTest extends MIDlet implements CommandListener{

	Display display;
	RecordStore recordStore;
	Command exitCmd;
	Command deleteCmd;
	Command addCmd;
	Command rebuildCmd;
	Command nextCmd;
	Command prevCmd;
	Command resetCmd;
	Command keepUpdatedCmd;
	TextBox currentRecord;
	byte[] data = {1, 2, 3};
	int index;
	int numOfRecords;
	RecordEnumeration renum;

	public EnumerationTest(){
		display = Display.getDisplay(this);
		currentRecord = new TextBox("目前的Record","",10, TextField.ANY);
		exitCmd = new Command("退出",Command.EXIT,1);
		deleteCmd = new Command("删除",Command.SCREEN,1);
		addCmd = new Command("新增",Command.SCREEN,2);
		nextCmd = new Command("下一笔",Command.SCREEN,3);
		prevCmd = new Command("前一笔",Command.SCREEN,4);
		rebuildCmd = new Command("重建",Command.SCREEN,5);
		resetCmd = new Command("还原",Command.SCREEN,6);
		keepUpdatedCmd = new Command("立即更新",Command.SCREEN,7);
		currentRecord.addCommand(exitCmd);
		currentRecord.addCommand(addCmd);
		currentRecord.addCommand(deleteCmd);
		currentRecord.addCommand(nextCmd);
		currentRecord.addCommand(prevCmd);
		currentRecord.addCommand(rebuildCmd);
		currentRecord.addCommand(resetCmd);
		currentRecord.addCommand(keepUpdatedCmd);
		currentRecord.setCommandListener(this);
		try{
			recordStore = RecordStore.openRecordStore("test", true);
			numOfRecords = recordStore.getNumRecords();
			if(numOfRecords == 0){
				for(int i=0;i<data.length;i++){
					byte[] record = new byte[1];
					record[0] = data[i];
					recordStore.addRecord(record, 0, 1);
				}
			}
			index = recordStore.getNextRecordID();
			renum = recordStore.enumerateRecords(null, null, false);
		}
		catch(Exception ex){}
	}

	public void startApp(){
		try{
			int id = renum.nextRecordId();
			currentRecord.setString("Record"+id);
		}
		catch(Exception ex){}
		display.setCurrent(currentRecord);
	}

	public void pauseApp(){
	}

	public void destroyApp(boolean unconditional){
		try{
			renum.destroy();
			recordStore.closeRecordStore();
		}
		catch(Exception ex){}
	}

	public void commandAction(Command c, Displayable d){
		if(c == exitCmd){
			destroyApp(true);
			notifyDestroyed();
		}
		else if(c == deleteCmd){
			String record = currentRecord.getString();
			int id = Integer.parseInt(record.substring(6));
			try{
				recordStore.deleteRecord(id);
			}
			catch(Exception ex){}
			display.setCurrent(currentRecord);
		}
		else if(c == addCmd){
			byte[] record = new byte[1];
		    record[0] = (byte)index;
			try{
				recordStore.addRecord(record, 0, 1);
			}
			catch(Exception ex){}
			index++;
			display.setCurrent(currentRecord);
		}
		else if(c == rebuildCmd){
			try{
				renum.rebuild();
				int id = renum.nextRecordId();
				currentRecord.setString("Record"+id);
			}
			catch(Exception ex){}
			display.setCurrent(currentRecord);
		}
		else if(c == prevCmd){
			try{
				int id = renum.previousRecordId();
				currentRecord.setString("Record"+id);
			}
			catch(Exception ex){}
			display.setCurrent(currentRecord);
		}
		else if(c == nextCmd){
			try{
				int id = renum.nextRecordId();
				currentRecord.setString("Record"+id);
			}
			catch(Exception ex){}
			display.setCurrent(currentRecord);
		}
		else if(c == resetCmd){
			try{
				renum.reset();
				int id = renum.nextRecordId();
				currentRecord.setString("Record"+id);
			}
			catch(Exception ex){}
			display.setCurrent(currentRecord);
		}
		else if(c == keepUpdatedCmd){
			renum.keepUpdated(true);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?