如何在j2me midlet的rms中删除后读取记录?

时间:2012-07-21 07:43:35

标签: java java-me midp netbeans-7 rms

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

2 个答案:

答案 0 :(得分:1)

  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

答案 1 :(得分:1)

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

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

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

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

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