AddrBookMIDlet.java:
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
public class AddrBookMIDlet extends MIDlet implements CommandListener{
private Display display;
String currentForm = "";
String dbname = "addrbook";
public AddrBookMIDlet(){
display = Display.getDisplay(this);
}
public void startApp(){
mainForm();
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
}
public void mainForm(){
currentForm = "mainForm";
List l = new List("我的电话本", Choice.IMPLICIT);
l.append("查看所有电话", null);
l.append("新增电话记录", null);
l.append("查找电话记录", null);
l.append("退出电话本", null);
l.setCommandListener(this);
display.setCurrent(l);
}
public void listAllForm(String search){
currentForm = "listAllForm";
//AddrBook addrBook = new AddrBook();
List l = new List("查看所有记录", Choice.IMPLICIT);
Command back = new Command("返回", Command.BACK, 0);
Command ok = new Command("查看", Command.OK, 0);
AddrBookVct addrBookVct = new AddrBookVct();
if (search == null)
addrBookVct = listAddrBook(null, 1);
else
addrBookVct = listAddrBook(search, 3);
l.addCommand(back);
l.addCommand(ok);
l.setCommandListener(this);
if (addrBookVct.size() == 0){
Alert a = new Alert("提示", "没有记录", null, null);
a.setTimeout(2000);
display.setCurrent(a);
return;
}
for (int i=0; i<addrBookVct.size(); i++){
l.append(addrBookVct.get(i).name, null);
}
display.setCurrent(l);
}
public void showTelForm(String name){
currentForm = "showTelForm";
AddrBookVct addrBookVct = listAddrBook(name, 2);
Command back = new Command("返回", Command.BACK, 0);
Form f = new Form("详细资料");
f.append("姓名:/n");
f.append(addrBookVct.get(0).name + "/n");
f.append("电话号码:/n");
f.append(addrBookVct.get(0).tel + "/n");
f.addCommand(back);
f.setCommandListener(this);
display.setCurrent(f);
}
public void addTelForm(){
currentForm = "addTelForm";
Form addForm = new Form("新增电话记录");
TextField tfName = new TextField("姓名", "", 20, TextField.ANY);
TextField tfTel = new TextField("电话", "", 20, TextField.NUMERIC);
Command back = new Command("返回", Command.BACK, 0);
Command ok = new Command("确定", Command.OK, 0);
addForm.append(tfName);
addForm.append(tfTel);
addForm.addCommand(ok);
addForm.addCommand(back);
addForm.setCommandListener(this);
display.setCurrent(addForm);
}
public void searchForm(){
currentForm = "searchForm";
TextBox t = new TextBox("请输入要查找的姓名", "", 20 ,0);
Command back = new Command("查找", Command.OK, 0);
Command search = new Command("返回", Command.OK, 0);
t.addCommand(back);
t.addCommand(search);
t.setCommandListener(this);
display.setCurrent(t);
}
public void commandAction(Command c, Displayable s){
if (c==List.SELECT_COMMAND && currentForm.equals("mainForm")){
List temp = (List)s;
switch(temp.getSelectedIndex()){
case 0:
listAllForm(null);
break;
case 1:
addTelForm();
break;
case 2:
searchForm();
break;
case 3:
destroyApp(false);
}
}
if (currentForm.endsWith("addTelForm")){
if (c.getLabel().equals("确定")){
Alert a = new Alert("提示", "添加电话记录成功", null, null);
a.setTimeout(2000);
Form Temp = (Form)s;
TextField tf1 = (TextField)Temp.get(0);
TextField tf2 = (TextField)Temp.get(1);
if (tf1.getString().equals("")){
a.setString("姓名不能为空");
display.setCurrent(a);
return;
}
if (tf2.getString().equals("")){
a.setString("电话不能为空");
display.setCurrent(a);
return;
}
addAddrBook(tf1.getString(), tf2.getString());
display.setCurrent(a);
tf1.setString("");
tf2.setString("");
}
if (c.getLabel().equals("返回")){
mainForm();
}
}
if (currentForm.equals("listAllForm")){
if (c.getLabel().equals("查看")){
List temp = (List)s;
showTelForm(temp.getString(temp.getSelectedIndex()));
}
if (c.getLabel().equals("返回")){
mainForm();
}
}
if (currentForm.equals("showTelForm")){
if (c.getLabel().equals("返回")){
mainForm();
}
}
if (currentForm.equals("searchForm")){
if (c.getLabel().equals("返回")){
mainForm();
}
if (c.getLabel().equals("查找")){
TextBox temp = (TextBox)s;
listAllForm(temp.getString());
}
}
}
public RecordStore openRSAnyway(String dbname){
RecordStore rs = null;
if (dbname.length()>32)
return null;
try{
rs = RecordStore.openRecordStore(dbname, true);
return rs;
}
catch (Exception e){
return null;
}
}
public void addAddrBook(String name, String tel){
FriendData data = new FriendData();
data.name = name;
data.tel = tel;
byte[] temp = data.encode();
RecordStore rs = openRSAnyway(dbname);
if (rs == null){
System.out.println("RecordStore is null");
return;
}
else {
try {
rs.addRecord(temp, 0, temp.length);
rs.closeRecordStore();
}
catch (Exception e){
}
}
}
public AddrBookVct listAddrBook(String listName, int Type){
RecordStore rs = openRSAnyway(dbname);
AddrBookVct addrBookVct = new AddrBookVct();
AddrBook addrBook = new AddrBook();
int i = 0;
if (rs == null){
System.out.println("ListAddrBook rs is null");
}
else {
try{
RecordEnumeration re = rs.enumerateRecords(null, null, false);
FriendData data = new FriendData();
if (re.numRecords() == 0){
addrBook.name = "No Name";
addrBook.tel = "No Tel";
addrBookVct.add(addrBook);
return addrBookVct;
}
while (re.hasNextElement()){
byte temp[] = re.nextRecord();
data.decode(temp);
addrBook.rsid = i++;
addrBook.name = data.name;
addrBook.tel = data.tel;
switch(Type){
case 1: //查看所有
addrBookVct.add(new AddrBook((String)data.name, (String)data.tel));
break;
case 2: //完全匹配查找
if (data.name.equals(listName)){
addrBookVct.add(new AddrBook((String)data.name, (String)data.tel));
}
break;
case 3: //模糊查找
String s = data.name.toUpperCase();
int search_i = s.indexOf(listName.toUpperCase());
if (search_i >= 0){
System.out.println("DATA Name: " + data.name);
addrBookVct.add(new AddrBook((String)data.name, (String)data.tel));
}
System.out.println("search_i: " + search_i + "DATA Name: " + data.name);
break;
}
}
}
catch (Exception e){
}
}
return addrBookVct;
}
}
AddrBook.java:
public class AddrBook {
int rsid;
String name;
String tel;
public AddrBook(){
rsid = 0;
name = "No Name";
tel = "No Tel";
}
public AddrBook(String name, String tel){
this.rsid = 0;
this.name = name;
this.tel = tel;
}
}
AddrBookVct.java:
import java.util.Vector;
public class AddrBookVct {
Vector vctTemp = new Vector();
AddrBook addrBook;
public void add(AddrBook a){
vctTemp.addElement(a);
}
public int size(){
return vctTemp.size();
}
public AddrBook get(int i){
AddrBook a;
a = (AddrBook)vctTemp.elementAt(i);
return a;
}
}
FriendData.java:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class FriendData {
String name;
String tel;
public FriendData(){
name = "No Name";
tel = "No Tel";
}
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();
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();
tel = dis.readUTF();
dis.close();
bis.close();
}
catch (Exception e){
}
}
}