Java me J2ME记录存储更新、删除操作出现问题
我创建一个列表,显示记录存储中的数据。我试图更新一条记录并重新显示列表(重新打开同一记录存储),但更新的项目没有改变(仍然包含旧数据)。Java me J2ME记录存储更新、删除操作出现问题,java-me,recordstore,Java Me,Recordstore,我创建一个列表,显示记录存储中的数据。我试图更新一条记录并重新显示列表(重新打开同一记录存储),但更新的项目没有改变(仍然包含旧数据)。 我还尝试删除一个项目,但删除的项目仍显示在列表中。 我使用NetBeans 7.0和JavaMeSDK 3.0中的emulator运行该程序 这是更新记录的代码 public void updateClient(Client cl) throws Exception{ RecordStore rs=RecordStore.openRecordS
我还尝试删除一个项目,但删除的项目仍显示在列表中。
我使用NetBeans 7.0和JavaMeSDK 3.0中的emulator运行该程序 这是更新记录的代码
public void updateClient(Client cl) throws Exception{
RecordStore rs=RecordStore.openRecordStore(String.valueOf(clientsStoreKey), true);
int recNum=rs.getNumRecords();
if (recNum>0){
RecordEnumeration renum=rs.enumerateRecords(null, null,false);
while(renum.hasNextElement()){
int id = renum.nextRecordId();
byte[] buff=rs.getRecord(id);
Client temp=Client.createFrom(buff);
if(temp.clientId.compareTo(cl.clientId)==0){
temp.firstName=cl.firstName;
temp.lastName=cl.lastName;
temp.city=cl.city;
temp.state=cl.state;
temp.company=cl.company;
temp.phone=cl.phone;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
temp.writeTo(dos);
byte[] sData=bos.toByteArray();
rs.setRecord(id, sData, 0, sData.length);
dos.close();
bos.close();
break;
}
}
renum.destroy();
}
rs.closeRecordStore();
}
这是获取记录的代码
public Vector getClients()
throws Exception{
RecordStore rs=RecordStore.openRecordStore(String.valueOf(clientsStoreKey), true);
int recNum=rs.getNumRecords();
Vector cls=new Vector();
if (recNum>0){
RecordEnumeration renum=rs.enumerateRecords(null, null,false);
while(renum.hasNextElement()){
byte[] buff=renum.nextRecord();
Client cl=Client.createFrom(buff);
cls.addElement(cl);
}
renum.destroy();
}
rs.closeRecordStore();
return cls;
}
有趣的是,我觉得你处理唱片店的代码还不错。用户界面是否有可能出现一些小问题,比如说使用旧的或更新不正确的屏幕对象
如何调试应用程序?既然您提到emulator,
System.out/println
看起来是一个自然的选择,不是吗?在updateClient
中设置记录后,以及在getClients
中获取记录后,我会使用它来输出记录的内容。谢谢您的回答。我终于成功地解决了这个问题。不知何故,我尝试通过删除记录、获取ID,然后添加带有更新数据的新记录来更新记录。