import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.lcdui.*;
public class MyPhoneBookUI extends MIDlet implements CommandListener{
private Display dis;
private List menu=new List("通讯簿",Choice.IMPLICIT);
private Command cmdSelect=new Command("选择",Command.OK,1);
private Command cmdExt=new Command("退出",Command.EXIT,1);
private Form addForm =new Form("添加新的联系人");
private TextField txtName = new TextField("姓名","", 20 , TextField.ANY); //姓名
private TextField txtTel = new TextField("电话","", 15, TextField.NUMERIC);//电话
private Command cmdBack = new Command("返回",Command.BACK,1);
private Command cmdSave = new Command("保存",Command.OK,2);
private RecordStore rs;
private RecordEnumeration re;
private String dbname="Book";
static final Command cmdDelete = new Command("删除", Command.SCREEN, 1);
static final Command cmdInfo= new Command("详细", Command.SCREEN, 1) ;
public MyPhoneBookUI() {
dis=Display.getDisplay(this);
}
public void addData(){
String name=txtName.getString();//获得用户输入的姓名
String tel=txtTel.getString();
try{
rs=RecordStore.openRecordStore(dbname, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
DataOutputStream os = new DataOutputStream(baos);
os.writeUTF(name);
os.writeUTF(tel);
os.writeInt(rs.getNextRecordID());//rs.getNextRecordID得到数据库中下一个可用的记录编号
byte[] data = baos.toByteArray( );
rs.addRecord(data, 0, data.length);
System.out.println("添加记录成功");
os.close();
baos.flush();
os.flush();
rs.closeRecordStore();
}
catch(Exception e){
e.printStackTrace();
}
}
//显示主界面
public void MainForm()
{
//选择列表
menu.append("全部通讯录", null) ;
menu.append("添加", null);
menu.addCommand(cmdSelect);
menu.setSelectCommand(cmdSelect);
menu.addCommand(cmdExt);
menu.setCommandListener(this);
dis.setCurrent(menu) ;
}
protected void destroyApp(boolean arg0) {
// TODO Auto-generated method stub
notifyDestroyed();
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
addForm.append(txtName) ;//將添加窗口做好
addForm.append(txtTel) ;
addForm.addCommand(cmdSave) ;
addForm.addCommand(cmdBack) ;
addForm.setCommandListener(this) ;
MainForm();
}
//显示添加联系人记录界面
public void AddForm()
{
dis.setCurrent(addForm);
}
//列举所有联系人记录
public void listAllForm()
{ //联系人列表
List myList = new List("所有联系人", Choice.EXCLUSIVE);
myList.addCommand(cmdBack);
myList.setCommandListener(this) ;
//从数据库中获取所有记录,动态添加到myList列表框中
try{
rs=RecordStore.openRecordStore(dbname, true);
re=rs.enumerateRecords(null, null, true);
if(re.numRecords()==0){
myList.append("没有记录", null);
}
else{
while(re.hasNextElement()){
byte[] Tmp=re.nextRecord();
ByteArrayInputStream baos = new ByteArrayInputStream(Tmp );
DataInputStream os = new DataInputStream(baos);
myList.append(os.readUTF(), null);
}
}
}
catch(Exception e){
e.printStackTrace();
}
myList.addCommand(cmdInfo);
myList.addCommand(cmdDelete);
dis.setCurrent(myList);
}
public void commandAction(Command c, Displayable d) {
//主屏幕的菜单选择
if(c==cmdExt){
destroyApp(true);
}
else{
if(c == cmdSelect){
List tmp = (List)d;
switch(tmp.getSelectedIndex()){
case 0 : listAllForm();//显示所有记录
break ;
case 1 : AddForm();//显示添加记录界面
break ;
}
}
if(c==cmdBack){
dis.setCurrent(menu);
}
if(c==cmdInfo){//查看某记录的详情
}
if(c==cmdDelete){//删除某记录
}
if(c==cmdSave){//保存记录
addData();
listAllForm();
}
}
}
}