本文实现了一个简单的J2ME记事本功能,本段代码经过测试并在手机中跑过:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.rms.* ;
public class MyAddressBook extends MIDlet implements CommandListener
{
private Display display;
String dbname = "MyAddressBook" ;
String current = "" ;
int record_id=-1;
Form addForm = new Form("新增数据") ;
TextField tf1 = new TextField("姓名","",20,TextField.ANY);
TextField tf2 = new TextField("电话","",10,TextField.NUMERIC);
public MyAddressBook()
{
display = Display.getDisplay(this);
}
public void startApp()
{
Command add = new Command("确定",Command.SCREEN,1) ;
Command back = new Command("后退",Command.SCREEN,2) ;
addForm.append(tf1) ;
addForm.append(tf2) ;
addForm.addCommand(add) ;
addForm.addCommand(back) ;
addForm.setCommandListener(this) ;
MainForm() ;
}
public void MainForm()
{
List l = new List(" 我的通信录",Choice.IMPLICIT) ;
l.append("通信录列表",null) ;
l.append("新增数据",null) ;
l.setCommandListener(this) ;
current = "MainForm" ;
display.setCurrent(l) ;
}
public void ListAllForm()
{
List l = new List("通信录列表",Choice.EXCLUSIVE) ;
Command back = new Command("后退",Command.SCREEN,2) ;
l.addCommand(back) ;
l.setCommandListener(this) ;
// try{
//RecordStore.deleteRecordStore(dbname);
// }catch(Exception e){}
RecordStore rs = openRSAnyway(dbname) ;
if( rs == null )
{
//启动失败停止MIDlet
notifyDestroyed();
}else
{
try
{
RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
FriendData data = new FriendData() ;
if(re.numRecords() == 0)
{
l.append("没有任何数据",null) ;
current = "ListAllForm" ;
display.setCurrent(l) ;
return ;
}
for(int i=0;i<re.numRecords();i++)
{
byte tmp[] = re.nextRecord() ;
data.decode(tmp) ;
l.append(data.name,null);
}
rs.closeRecordStore() ;
}catch(Exception e)
{
}
}
Command info = new Command("详细",Command.SCREEN,1) ;
l.addCommand(info) ;
current = "ListAllForm" ;
display.setCurrent(l) ;
}
public void AddForm()
{
current = "AddForm" ;
tf1.setString("");
tf2.setString("");
display.setCurrent(addForm) ;
}
public void commandAction(Command c,Displayable s)
{
int temp=0;
if(c == List.SELECT_COMMAND && current.equals("MainForm"))
{
List tmp = (List) s ;
switch(tmp.getSelectedIndex())
{
case 0 :
ListAllForm() ;
break ;
case 1 :
AddForm() ;
break ;
}
}
if(c.getLabel().equals("后退"))
{
MainForm() ;
}
if(c.getLabel().equals("确定"))
{
addData() ;
ListAllForm() ;
}
if(c.getLabel().equals("详细"))
{
List tmp = (List) s ;
searchData(tmp.getString(tmp.getSelectedIndex())) ;
}
if(c.getLabel().equals("删除"))
{
RecordStore rs = openRSAnyway(dbname) ;
if( rs == null )
{
//启动失败停止MIDlet
notifyDestroyed();
}else
{
try
{
RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
FriendData data = new FriendData() ;
for(int i=0;i<re.numRecords();i++)
{
temp=re.nextRecordId();
byte tmp[] = rs.getRecord(temp);
data.decode(tmp) ;
if(temp==record_id)
{
rs.deleteRecord(temp);
this.record_id=-1;
ListAllForm();
return ;
}
}
rs.closeRecordStore() ;
}catch(Exception e)
{
e.printStackTrace();
}
}
ListAllForm() ;
// List tmp = (List) s ;
// searchData(tmp.getString(tmp.getSelectedIndex())) ;
}
}
public void searchData(String name)
{
int temp=0;
Form f = new Form("详细数据") ;
Command back = new Command("后退",Command.SCREEN,1) ;
Command delete = new Command("删除",Command.SCREEN,1) ;
f.addCommand(back) ;
f.addCommand(delete) ;
f.setCommandListener(this) ;
RecordStore rs = openRSAnyway(dbname) ;
if( rs == null )
{
//启动失败停止MIDlet
notifyDestroyed();
}else
{
try
{
RecordEnumeration re = rs.enumerateRecords(null,null,false) ;
FriendData data = new FriendData() ;
for(int i=0;i<re.numRecords();i++)
{
temp=re.nextRecordId();
byte tmp[] = rs.getRecord(temp);
data.decode(tmp) ;
if(data.name.equals(name))
{
record_id=temp;
f.append("姓名: /n") ;
f.append(data.name+"/n") ;
f.append("电话: /n") ;
f.append(data.tel+"/n") ;
display.setCurrent(f) ;
return ;
}
}
rs.closeRecordStore() ;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public void addData()
{
FriendData data = new FriendData() ;
data.name = tf1.getString() ;
data.tel = tf2.getString() ;
byte []tmp = data.encode() ;
RecordStore rs = openRSAnyway(dbname) ;
if( rs == null )
{
//启动失败停止MIDlet
notifyDestroyed();
}else
{
try
{
rs.addRecord(tmp,0,tmp.length) ;
rs.closeRecordStore() ;
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public RecordStore openRSAnyway(String rsname)
{
RecordStore rs = null ;
//名称大于32个字符就不接受
if(rsname.length() > 32)
return null ;
try
{
rs = RecordStore.openRecordStore(rsname,true) ;
return rs ;
}catch(Exception e)
{
return null ;
}
}
}
class FriendData
{
String name ;
String tel ;
public FriendData()
{
name = "NO NAME" ;
name = "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)
{
}
}
}