2

我在我的 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;
}
4

2 回答 2

1

尽管gnat是完全正确的,但没有人使用 keepUpdated 参数是有充分理由的(依赖于 MIPD 规范的一个非常糟糕的部分,失去对同步的控制......)。

如果您想控制应用程序的行为,keepUpdated 应该保持为 false,并且您的 AddCustomer() 方法应该关闭 RecordStore。

然后,您需要一种触发 GUI 更新的机制(通过重新执行整个 getChoiceGroup10() 方法并在屏幕上显示新结果),因为 AddCustomer() 已成功调用。为了正确地做到这一点,您需要深入了解您的 GUI 线程模型。你使用 LWUIT 吗?您是否为您的 GUI 创建了一个线程?GUI 是仅在必要时刷新还是有帧速率?

基于这种理解,您将要么使用侦听器接口,使用同步,设置标志以稍后更新屏幕的一部分......

通常,您不希望在屏幕上绘制的线程也从 RecordStore 读取,因此您实际上可能最终重写您的 getChoiceGroup10() 方法,以便将其内容拆分为 2 个线程。

于 2012-07-21T12:25:02.590 回答
1
  recEnum = rs19.enumerateRecords( null, null, false ); // --> keepUpdated is false

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

要了解为什么会这样并了解如何使用方法参数,请参阅 API 文档(在线提供)-注意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
于 2012-07-21T08:48:31.987 回答