import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class rmsmidlet extends midlet implements commandlistener {
private display display;
private ticker news;
private command exitcommand;
private command execommand;
private command savecommand;
private command delcommand;
private list menu;
private string[] menuitem = {"通讯录清单","新增资料","删除资料","修改资料"};
private list listaddress;
private form form;
textfield namefield = new textfield("姓名","",20,textfield.any);
textfield emailfield = new textfield("电邮","",20,textfield.any);
textfield telfield = new textfield("电话","",20,textfield.numeric);
string dbname = new string("rms");
private string current = new string("");
//结构化
public rmsmidlet() {
news = new ticker("号外:元旦只放一天!");
exitcommand = new command("离开",command.exit,1);
execommand = new command("确定",command.screen,2);
savecommand = new command("保存",command.screen,2);
delcommand = new command("删除",command.screen,2);
display = display.getdisplay(this);
}
//初始化
public void startapp() {
menu = new list(null,list.implicit,menuitem,null);
menu.setticker(news);
menu.addcommand(exitcommand);
menu.addcommand(execommand);
menu.setcommandlistener(this);
current = "主菜单";
display.setcurrent(menu);
}
//终止
public void pauseapp() {}
//销毁
public void destroyapp(boolean unconditional) {}
//按钮监听
public void commandaction(command c,displayable s) {
if(c == exitcommand && current.equals("主菜单")) {
destroyapp(false);
notifydestroyed();
}
if(c == exitcommand && current.equals("通讯录清单")) {
startapp();
}
if(c == exitcommand && current.equals("新增资料")) {
startapp();
}
if(c == exitcommand && current.equals("删除资料")) {
startapp();
}
if(c == exitcommand && current.equals("修改资料")) {
startapp();
}
if(c == exitcommand && current.equals("详细资料")) {
listadress();
}
if(c == savecommand) {
adddata();
startapp();
}
if(c == delcommand) {
deleters(dbname);
startapp();
}
if(c == execommand && current.equals("通讯录清单")) {
list tmp = (list) s ;
searchdata(tmp.getstring(tmp.getselectedindex())) ;
}
if(c == execommand && current.equals("修改资料")) {
//startapp();
}
if((c == list.select_command && current.equals("主菜单"))||(c.getlabel().equals("确定"))) {
list tmp = (list) s;
switch(tmp.getselectedindex()) {
case 0:
listadress();
break;
case 1:
addadress();
break;
case 2:
deladress();
break;
case 3:
modifyadress();
break;
}
}
}
//显示清单
public void listadress() {
listaddress = new list("通讯录清单",list.implicit);
listaddress.addcommand(exitcommand);
listaddress.addcommand(execommand);
listaddress.setcommandlistener(this);
recordstore rs = openrs(dbname) ;
if(rs == null) {
notifydestroyed();
}
else {
try {
recordenumeration re = rs.enumeraterecords(null,null,false) ;
addressdata data = new addressdata() ;
if(re.numrecords() == 0) {
listaddress.append("沒有任何数据",null) ;
current = "通讯录清单" ;
display.setcurrent(listaddress) ;
return;
}
while(re.hasnextelement()) {
byte[] tmp = re.nextrecord() ;
data.decode(tmp) ;
listaddress.append(data.name,null) ;
}
rs.closerecordstore() ;
}
catch(exception e) { }
}
current = "通讯录清单";
display.setcurrent(listaddress);
}
//详细资料
public void searchdata(string name) {
form = new form("详细资料") ;
form.addcommand(exitcommand) ;
form.setcommandlistener(this) ;
recordstore rs = openrs(dbname) ;
if( rs == null ) {
notifydestroyed();
}
else {
try {
recordenumeration re = rs.enumeraterecords(null,null,false) ;
addressdata data = new addressdata() ;
while(re.hasnextelement()) {
byte[] tmp = re.nextrecord() ;
data.decode(tmp) ;
if(data.name.equals(name)) {
form.append("姓名: \n") ;
form.append(data.name+"\n") ;
form.append("电邮: \n") ;
form.append(data.email+"\n") ;
form.append("电话: \n") ;
form.append(data.tel+"\n") ;
display.setcurrent(form) ;
return ;
}
}
rs.closerecordstore() ;
}
catch(exception e) { }
}
}
//新增资料界面
public void addadress() {
form = new form("新增资料");
form.append(namefield);
form.append(emailfield);
form.append(telfield);
form.addcommand(exitcommand);
form.addcommand(savecommand);
form.setcommandlistener(this);
current = "新增资料";
display.setcurrent(form);
}
//新增资料
public void adddata() {
addressdata data = new addressdata();
data.name = namefield.getstring();
data.email = emailfield.getstring() ;
data.tel = telfield.getstring() ;
byte[] tmp = data.encode() ;
recordstore rs = openrs(dbname) ;
if( rs == null ) {
notifydestroyed();
}
else {
try {
rs.addrecord(tmp,0,tmp.length) ;
rs.closerecordstore() ;
}
catch(exception e) { }
}
}
//删除资料界面
public void deladress() {
form = new form("删除资料");
form.append("你确定要删除全部资料吗?");
form.addcommand(exitcommand);
form.addcommand(delcommand);
form.setcommandlistener(this);
current = "删除资料";
display.setcurrent(form);
}
//修改资料界面
public void modifyadress() {
listaddress = new list("修改资料",list.implicit);
listaddress.addcommand(exitcommand);
listaddress.addcommand(execommand);
listaddress.setcommandlistener(this);
recordstore rs = openrs(dbname) ;
if(rs == null) {
notifydestroyed();
}
else {
try {
recordenumeration re = rs.enumeraterecords(null,null,false) ;
addressdata data = new addressdata() ;
if(re.numrecords() == 0) {
listaddress.append("沒有任何数据",null) ;
current = "修改资料" ;
display.setcurrent(listaddress) ;
return;
}
while(re.hasnextelement()) {
byte[] tmp = re.nextrecord() ;
data.decode(tmp) ;
listaddress.append(data.name,null) ;
}
rs.closerecordstore() ;
}
catch(exception e) { }
}
current = "修改资料";
display.setcurrent(listaddress);
}
//打开或创建数据库
public recordstore openrs(string dbtable) {
recordstore rs = null ;
if(dbtable.length() > 32)
return null ;
try {
rs = recordstore.openrecordstore(dbtable,true) ;
return rs ;
}
catch(exception e) {
return null ;
}
}
//删除数据库
public boolean deleters(string dbtable) {
if(dbtable.length() > 32)
return false ;
try {
recordstore.deleterecordstore(dbtable) ;
}
catch(exception e) {
return false ;
}
return true ;
}
}
class addressdata {
string name;
string email;
string tel;
public addressdata() {
name = "no name";
email = "no email";
tel = "no tel";
}
public byte[] encode() {
byte[] result = null;
try {
bytearrayoutputstream bos = new bytearrayoutputstream();
dataoutputstream dos = new dataoutputstream (bos);
dos.writeutf(name);
dos.writeutf(email);
dos.writeutf(tel);
result = bos.tobytearray();
dos.close();
bos.close();
}
catch(exception e) { }
return result;
}
public void decode(byte[] data) {
try {
bytearrayinputstream bis = new bytearrayinputstream(data);
datainputstream dis = new datainputstream (bis);
name = dis.readutf();
email = dis.readutf();
tel = dis.readutf();
dis.close();
bis.close();
}
catch(exception e) { }
}
}
