#region 調用RMS類,顯示電話本
package Example3.Test;
import Java.util.Vector;
import Javax.microedition.lcdui.Command;
import Javax.microedition.lcdui.CommandListener;
import Javax.microedition.lcdui.Displayable;
import Javax.microedition.lcdui.List;
public class PhonesList extends List implements CommandListener {
private Phones p;
private Command Back=new Command("返回",Command.BACK,1);
private Command add=new Command("添加",Command.SCREEN,1);
private Command del=new Command("刪除",Command.SCREEN,1);
public PhonesList(Phones ph)
{
super("電話本",List.IMPLICIT);
this.p=ph;
this.addCommand(Back);
this.addCommand(add);
this.addCommand(del);
this.setCommandListener(this);
}
public void LoadPhoneList() {
// TODO Auto-generated method stub
Vector v= RMSOpen.GetAllRMS();
System.out.println(v.size());
for(int i=0;i<v.size();i++)
{
this.append((String)v.elementAt(i),null);
}
RMSOpen.CloseRMS();
}
private void DeletePhone()
{
RMSOpen.deleteRMS(this.getString(this.getSelectedIndex()));
RMSOpen.CloseRMS();
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==Back)
{
p.Chand("Main");
}
else if(c==add)
{
p.Chand("addPhone");
}
else if(c==del)
{
this.DeletePhone();
p.Chand("PhonesList");
}
}
}
#endregion
#region 添加電話操作 調用RMS類
package Example3.Test;
import Javax.microedition.lcdui.Command;
import Javax.microedition.lcdui.Displayable;
import Javax.microedition.lcdui.Form;
import Javax.microedition.lcdui.TextFIEld;
public class addPhone extends Form implements Javax.microedition.lcdui.CommandListener {
private Phones P;
private TextField tname=new TextField("姓名:","",255,TextFIEld.ANY);
private TextField phone =new TextField("電話:","",255,TextFIEld.PHONENUMBER);
private Command ok=new Command("確定",Command.OK,1);
private Command back=new Command("返回",Command.BACK,1);
addPhone(Phones p)
{
super("添加電話");
this.P=p;
this.append(tname);
this.append(phone);
this.addCommand(ok);
this.addCommand(back);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==back)
{
P.Chand("PhonesList");
}
else if(c==ok)
{
RMSOpen.AddRMS(this.tname.getString(), phone.getString());
RMSOpen.CloseRMS();
P.Chand("PhonesList");
}
}
}
#endregion
#region 對RMS的操作
package Example3.Test;
import Java.io.UnsupportedEncodingException;
import Java.util.Vector;
import Javax.microedition.rms.InvalidRecordIDException;
import Javax.microedition.rms.RecordStoreException;
import Javax.microedition.rms.RecordStoreFullException;
import Javax.microedition.rms.RecordStoreNotFoundException;
import Javax.microedition.rms.RecordStoreNotOpenException;
public class RMSOpen {
static Javax.microedition.rms.RecordStore rs;
public static void OpenRMS()
{
try {
rs=Javax.microedition.rms.RecordStore.openRecordStore("phonelist", true);
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void CloseRMS()
{
try {
rs.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deleteRMS(String str)
{
OpenRMS();
int ID = 0;
try {
ID=rs.getNextRecordID();
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=1;i<ID;i++)
{
byte[] by = null;
try {
by = rs.getRecord(i);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name=new String(by);
if(name.equals(str))
{
try {
rs.deleteRecord(i);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void AddRMS(String name,String Phone)
{
OpenRMS();
String np=name+":"+Phone;
byte[] by = np.getBytes();
try {
int k=rs.addRecord(by,0,by.length);
System.out.println(k);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void UpdateRMS()
{}
public static Vector GetAllRMS()
{
OpenRMS();
Vector v=new Vector();
try {
Javax.microedition.rms.RecordEnumeration ren=rs.enumerateRecords(null,null, false);
while(ren.hasNextElement())
{
try {
v.addElement(new String(ren.nextRecord()));
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
try {
rs.openRecordStore("phonelist", true);
} catch (RecordStoreFullException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return v;
}
}
上進,務實Tag標簽: J2ME,RMS,Vector