2012-07-21 56 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個線程。