📄 addressbookmidlet.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import java.io.*;
public class AddressBookMIDlet extends MIDlet implements CommandListener{
private Display display;
private String current = null;
int[] recordes;
String dbname = "dressbook";
Form addform = new Form("增加资料");
TextField tfname = new TextField("姓名:","",20,TextField.ANY);
TextField tftel = new TextField("电话:","",20,TextField.PHONENUMBER);
public AddressBookMIDlet(){
display = Display.getDisplay(this);
}
public void startApp(){
//必须先初始化,不能放到addForm中再初始化。
Command add = new Command("确定",Command.OK,1);
Command back= new Command("返回",Command.BACK,3);
addform.append(tfname);
addform.append(tftel);
addform.addCommand(add);
addform.addCommand(back);
addform.setCommandListener(this);
mainForm();
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c,Displayable d){
if(c == List.SELECT_COMMAND && current.equals("mainForm")){
List temp = (List) d;
switch (temp.getSelectedIndex()){
case 0:
searchData();
break;
case 1:
addForm();
break;
case 2:
delForm();
break;
}
}
if(current.equals("addForm")){
if (c.getLabel().equals("确定")) addData();
if (c.getLabel().equals("返回")) mainForm();
}
if(current.equals("listallForm")){
if(c.getLabel().equals("返回")) mainForm();
}
if(current.equals("delForm")){
if(c.getLabel().equals("返回")) mainForm();
if(c.getLabel().equals("删除")) {
List temp = (List) d;
deleteData(temp.getSelectedIndex());
delForm();
}
}
}
//主操作界面
public void mainForm(){
current = "mainForm";
List l = new List("我的电话本",Choice.IMPLICIT);
l.append("查找",null);
l.append("增加",null);
l.append("删除",null);
l.append("分组信息",null);
l.append("存储状况",null);
l.setCommandListener(this);
display.setCurrent(l);
}
//增加电话号码界面
public void addForm(){
current = "addForm";
display.setCurrent(addform);
}
//删除电话界面
public void delForm(){
current = "delForm";
List flist = new List("删除电话:",Choice.EXCLUSIVE);
Command confirm = new Command("删除",Command.SCREEN,1);
Command back = new Command("返回",Command.BACK,2);
flist.addCommand(confirm);
flist.addCommand(back);
flist.setCommandListener(this);
listData(flist);
}
//查找数据
public void searchData(){
List flist = new List("电话列表:",Choice.EXCLUSIVE);
current = "listallForm";
Command back = new Command("返回",Command.SCREEN,1);
flist.addCommand(back);
flist.setCommandListener(this);
listData(flist);
}
//增加数据
public void addData(){
//如果输入为空,则提示出错!
if(tfname.getString().trim().equals("")||tftel.getString().equals("")){
Alert al = new Alert("Input null");
al.setType(AlertType.ERROR);
al.setString("输入不能为空!");
al.setTimeout(1500);
display.setCurrent(al);
return;
}
outinData data = new outinData();
data.name = tfname.getString();
data.tel = tftel.getString();
RecordStore rs = openRs(dbname);
byte[] temp = data.encode();
if(rs==null){
System.out.println("When Add Data,open Rs error!");
}else{
try{
rs.addRecord(temp,0,temp.length);
rs.closeRecordStore();
//deleteRs(dbname);
Alert al = new Alert("Add Record");
al.setType(AlertType.INFO);
al.setString("添加成功!");
al.setTimeout(1500);
display.setCurrent(al);
}catch(Exception e){System.out.println("Add data error");}
}
}
//电话列表
public void listData(List flist){
RecordStore rs = openRs(dbname);
if(rs==null){
System.out.println("When list Data,openRs error!");
}else{
try{
RecordEnumeration re = rs.enumerateRecords(null,null,false);
outinData data = new outinData();
int totalrecords = re.numRecords();
if(totalrecords==0){
flist.append("没有资料!",null);
display.setCurrent(flist);
return;
}
//把recordid读入数组
recordes = new int[totalrecords];
for(int i = 0;i<totalrecords;i++){
int j = re.nextRecordId();
recordes[i] = j;
byte[] temp = rs.getRecord(j);
data.decode(temp);
flist.append(data.name+" "+data.tel,null);
}
display.setCurrent(flist);
}catch(Exception e){System.out.println("When list Data,List error");}
}
}
//删除某条记录, int i为List对应的被选定的Item
public void deleteData(int i){
RecordStore rs = openRs(dbname);
System.out.println(i);
int id = recordes[i];
System.out.println("id:"+id);
try{
rs.deleteRecord(id);
}catch(Exception e){
System.out.println("delete Record error:"+e.getMessage());
return;
}
Alert al = new Alert("delete data");
al.setType(AlertType.ERROR);
al.setString("id"+id+" is deleted!");
al.setTimeout(1500);
display.setCurrent(al);
}
//打开Rs
public RecordStore openRs(String rsname){
RecordStore rs = null;
try{
rs = RecordStore.openRecordStore(rsname,true);
}catch(Exception e){
System.out.println("open Rs error:"+e.getMessage());
return null;
}
return rs;
}
//删除Rs
public boolean deleteRs(String rsname){
if(rsname.length()>32){
return false;
}
try{
RecordStore.deleteRecordStore(rsname);
}catch(Exception e){
System.out.println("deleteRs error:"+e.getMessage());
return false;
}
return true;
}
}
//按照rms要求的格式读出和写入数据
class outinData {
String name = null;
String tel = null;
public void decode(byte[] data){
try{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bis);
name = dis.readUTF();
tel = dis.readUTF();
bis.close();
dis.close();
}catch(Exception e){System.out.println("decode error:"+e.getMessage());}
}
public byte[] encode(){
byte[] result = null;
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(name);
dos.writeUTF(tel);
result = bos.toByteArray();
bos.close();
dos.close();
}catch(Exception e){
System.out.println("encode error:"+e.getMessage());
return null;
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -