【问题标题】:How to read records after delete in rms in j2me midlet?如何在 j2me midlet 中删除 rms 后读取记录?
【发布时间】:2012-07-21 07:43:34
【问题描述】:

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 我也尝试过您的建议,但没有得到。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多