⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordoperation.java

📁 java手机程序开发随书光盘源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class RecordOperation extends MIDlet implements CommandListener{

	Display display;
	RecordStore rs;
	TextBox editor;
	List list;
	Command exitCmd;
	Command deleteCmd;
	Command addCmd;
	Command cancelCmd;
	Command renameCmd;
	String professor ="阿辉";
	String student ="扁扁";
	byte[] test;
	Alert alert;
	int mode;

	static final int ADD = 1;
	static final int MODIFY = 2;

	public RecordOperation(){
		display = Display.getDisplay(this);
		try{
			rs = RecordStore.openRecordStore("School",true);
		}
		catch(RecordStoreException rse){}
		list = new List("学校成员",List.EXCLUSIVE);
		editor = new TextBox("请输入姓名","",10,
                                  TextField.EMAILADDR);
        alert = new Alert("信息");
		addCmd = new Command("新增",Command.SCREEN,1);
		deleteCmd = new Command("删除",Command.SCREEN,2);
		renameCmd = new Command("修改",Command.SCREEN,3);
		exitCmd = new Command("退出",Command.EXIT,1);
		cancelCmd = new Command("取消",Command.CANCEL,1);
		list.addCommand(addCmd);
		list.addCommand(deleteCmd);
		list.addCommand(renameCmd);
		list.addCommand(exitCmd);
		list.setCommandListener(this);
		editor.addCommand(addCmd);
		editor.addCommand(cancelCmd);
		editor.setCommandListener(this);
		mode = ADD;
		try{
			if(rs.getNumRecords() == 0){
				writeData(professor);
				writeData(student);
			}
			for(int i=1;i<=rs.getNumRecords();i++){
				String name = readData(i);
				list.append(name, null);
			}
		}
		catch(RecordStoreException rse){}

	}

	public void startApp(){
		display.setCurrent(list);
	}

	public void pauseApp(){
	}

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

	public void commandAction(Command c,Displayable d){
		if(c == addCmd){
			if(display.getCurrent() == list){
				mode = ADD;
				display.setCurrent(editor);
			}
			else if(display.getCurrent() == editor){
				if(mode == ADD){
					writeData(editor.getString());
					list.append(readData(list.size()+1),null);
				}
				else if(mode == MODIFY){
					int index = list.getSelectedIndex();
					writeData(editor.getString());
					list.set(index,readData(index+1),null);
				}
				display.setCurrent(list);
			}
		}
		else if(c == deleteCmd){
			try{
				rs.deleteRecord(list.getSelectedIndex());
				list.set(list.getSelectedIndex(), "退学", null);
				display.setCurrent(list);
			}
			catch(RecordStoreException rse){
				alert.setString("该同学已被退学");
				display.setCurrent(alert, list);
			}
		}
		else if(c == renameCmd){
			mode = MODIFY;
			display.setCurrent(editor);
		}
		else if(c == cancelCmd){
			display.setCurrent(list);
		}
		else if(c == exitCmd){
			destroyApp(true);
			notifyDestroyed();
		}
	}

	public void writeData(String dataToWrite){
		char[] tempData = dataToWrite.toCharArray();
		byte[] data = new byte[2*tempData.length];
		for(int i=0;i<tempData.length;i++){
			data[2*i] = (byte)((tempData[i] >> 8)&0xff);
			data[2*i+1] = (byte)(tempData[i]&0xff);
		}
		try{
			if(mode == ADD)
				rs.addRecord(data,0,data.length);
			else if(mode == MODIFY)
				rs.setRecord(list.getSelectedIndex()+1,data,0,data.length);
		}
		catch(RecordStoreException rse){}
	}

	public String readData(int recordId){
		byte[] data;
		String name = "";
		try{
			data = rs.getRecord(recordId);
			for(int i=0;i<data.length/2;i++){
				name+=(char)((char)(data[2*i]<<8)+(char)(data[2*i+1]));
			}
		}catch(RecordStoreException rse){}
		return name;
	}
}

⌨️ 快捷键说明

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