gpt4 book ai didi

java - j2me midlet中rms删除后如何读取记录?

转载 作者:行者123 更新时间:2023-11-30 09:34:59 26 4
gpt4 key购买 nike

我在我的 j2me 应用程序中使用 rms 概念。

第一条记录成功添加到rms,然后读取记录也很好但是当我要同时添加另一条记录时,它不会添加到我的列表中,它已成功添加到rms但当时没有刷新.

如果我退出应用程序一次然后运行记录读取良好的应用程序,所有记录都会显示。如何刷新列表并获取所有记录?

这是我添加记录的来源:

public void AddCustomer(String str) throws RecordStoreNotOpenException
{
byte[] rec=str.getBytes();
try
{
rs19 = RecordStore.openRecordStore(ADDREMOVE_CUSTOMER, true);
rs19.addRecord(rec, 0, rec.length);
System.out.println("REcord added successfully");
v.addElement(str);
}
catch (Exception e)
{
}
}

这是阅读记录的来源:

public ChoiceGroup getChoiceGroup10() {
if (choiceGroup10 == null) {
choiceGroup10 = new ChoiceGroup("Select Customer", Choice.POPUP);
choiceGroup10.append("none", null);
try
{
rs19.openRecordStore(ADDREMOVE_CUSTOMER, true);
String value="";
String comma=",";
Vector v1=new Vector();
StringBuffer enumList=new StringBuffer();
recEnum = rs19.enumerateRecords( null, null, false );
while( recEnum.hasNextElement() )
{
byte[] data = recEnum.nextRecord();
enumList.append(new String(data));
enumList.append(",");
}
records=new String(enumList);
int index=records.indexOf(comma);
while(index>=0)
{
v1.addElement(records.substring(0,index));
records = records.substring(index+comma.length());
index = records.indexOf(comma);
}
v1.addElement( records );
if( v1.size()>0 )
{
for(int i=0; i<v1.size(); i++)
{
value= (String)v1.elementAt(i);
choiceGroup10.append(value, null);
}
}
}
catch(InvalidRecordIDException ie)
{
ie.printStackTrace();
}
catch(RecordStoreException re)
{
re.printStackTrace();
}
catch(NullPointerException ne)
{
System.out.println(ne);
}
finally
{
try {
rs19.closeRecordStore();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}

}
return choiceGroup10;
}

最佳答案

  recEnum = rs19.enumerateRecords( null, null, false ); // --> keepUpdated is false

您描述的方式,成功添加到 rms 但当时没有刷新 似乎由传递给 enumerateRecords 的第三个参数定义。

要了解为什么会这样并学习如何使用方法参数,请参阅 API 文档 (available online) - 注意 keepUpdated 参数的解释:

public RecordEnumeration enumerateRecords(RecordFilter filter,
RecordComparator comparator,
boolean keepUpdated)
throws RecordStoreNotOpenException

Returns an enumeration for traversing a set of records in the record store
in an optionally specified order...

Parameters:
filter - if non-null, will be used to determine what subset
of the record store records will be used
comparator - if non-null, will be used to determine the order
in which the records are returned
keepUpdated - if true, the enumerator will keep its enumeration
current with any changes in the records of the record store.
Use with caution as there are possible performance consequences.
If false the enumeration will not be kept current and may return
recordIds for records that have been deleted or miss records
that are added later. It may also return records out of order
that have been modified after the enumeration was built.
Note that any changes to records in the record store are
accurately reflected when the record is later retrieved,
either directly or through the enumeration. The thing that is
risked by setting this parameter false is the filtering and
sorting order of the enumeration when records are modified,
added, or deleted.
...

如上所示,考虑使用 keepUpdated 参数的另一个值测试您的 MIDlet:

  recEnum = rs19.enumerateRecords(null, null, true); // --> test with 'true' here

关于java - j2me midlet中rms删除后如何读取记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11590412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com