2012-07-21 58 views
2

我在我的j2me应用程序中使用rms概念。如何在j2me midlet中删除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; 
} 

回答

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

您所描述的方式,加入到有效值成功,但没有刷新时时间似乎是由传递给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 
+0

我也试过你的建议,但没有得到。 – cheliyan 2012-07-21 09:34:51

1

虽然gnat是完全正确的,也有很好的理由,为什么没有人使用keepUpdated参数(依赖于MIPD规范中非常不好的部分,失去了对同步的控制......)。

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

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

基于这一认识,您将使用一个监听器接口,使用同步,设置一个标志,更新后的画面的一部分...

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