基于j2me的手机应用程序-电话本
源代码在线查看: findrecord.java
package PhoneBook;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
public class FindRecord extends Form implements CommandListener {
private Displayable dis;
private MIDlet mid;
private RecordStore rs = null;
private RecordEnumeration re = null;
private static final Command findCommand = new Command("查找", Command.OK, 1);
private static final Command backCommand = new Command("返回", Command.BACK,
2);
private TextField tf;
public FindRecord(Displayable dis,MIDlet mid,RecordStore rs) {
super("查询记录");
tf = new TextField("输入名字",null, 120,TextField.ANY);
//设置控件的布局
tf.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.append(tf);
this.dis = dis;
this.mid = mid;
this.rs = rs;
this.addCommand(backCommand);
this.addCommand(findCommand);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if (c == backCommand) {
Display.getDisplay(mid).setCurrent(dis);
}
if (c == findCommand) {
RecordFilter rf = new FindFilter(tf.getString());
RecordEnumeration re = null;
try {
re = rs.enumerateRecords(rf, null, false);
} catch (Exception e) {
}
//查找成功
if (re.numRecords() > 0) {
BookAccount account = null;
int recordID;
try {
byte[] rec;
recordID = re.nextRecordId();
rec = rs.getRecord(recordID);
account = BookAccount.deserialize(rec);
StringItem name = new StringItem("姓名:",account.getUserName());
StringItem mobilePhone = new StringItem("移动电话:",account.getMobilePhone());
StringItem phone = new StringItem("家庭电话:",account.getPhone());
StringItem email = new StringItem("电子邮件:",account.getEmail());
name.setLayout(Item.LAYOUT_NEWLINE_AFTER);
mobilePhone.setLayout(Item.LAYOUT_NEWLINE_AFTER);
phone.setLayout(Item.LAYOUT_NEWLINE_AFTER);
email.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.append(name);
this.append(mobilePhone);
this.append(phone);
this.append(email);
account = null;
re.destroy();
} catch (Exception e) {
account = null;
System.out.println("deserialize error");
}
}
}
}
}