2012-03-12 159 views
0

我現在面臨的存儲持久性存儲數據的問題,我試圖來存儲不同的日期事件持久性存儲,但數據是越來越重寫代碼:在持久性存儲中存儲數據

public ListEventScreen(Vector v,String timezone) { 
    for(int i=0;i<v.size();i++){ 
     EventBean bean=(EventBean)v.elementAt(i); 
     //a normal label in the app, just to display text, anchored left 
     LabelField label = new LabelField(bean.getSummary(),LabelField.FIELD_LEFT); 
     //add the label to the screen 
     add(label); 
     saveUserInfo(v); 
    } 
} 

public void saveUserInfo(Vector vectorData){ 
    // static{ 
     store = PersistentStore.getPersistentObject(0x1dfc10ec9447eb14L); 
     synchronized(store) { 
      store.setContents(vectorData); 
      store.commit(); 
     } 
    //} 
} 

請讓我知道必須改變什麼。

回答

1

每次調用store.setContents()時,persistentStore的當前內容被改寫爲Vector你逝去。你需要確保你加載那些已經在persistentStore以前的事件到您的Vector之前再加入新進入該Vector事件,然後你保存。

您還在ListEventScreen()的循環的每次迭代中調用saveUserInfo()。相反,你應該在循環之外調用它。

我會做這樣的事情:

public ListEventScreen(Vector v,String timezone) { 
    Enumeration e = v.elements();; 
    while (e.hasMoreElements()){ 
     EventBean bean = (EventBean) e.nextElement(); 
     //a normal label in the app, just to display text, anchored left 
     LabelField label = new LabelField(bean.getSummary(),LabelField.FIELD_LEFT); 
     //add the label to the screen 
     add(label); 
    } 
} 

public void loadUserInfo(Vector vectorData){ 
    // static{ 
     store = PersistentStore.getPersistentObject(0x1dfc10ec9447eb14L); 
     synchronized(store) { 
      Vector v = (Vector) store.getContents(); 
      Enumeration e = v.elements(); 
      while (e.hasMoreElemens){ 
       vectorData.add(e.nextElement()); 
      } 
     } 
    //} 
} 

public void saveUserInfo(Vector vectorData){ 
    // static{ 
     store = PersistentStore.getPersistentObject(0x1dfc10ec9447eb14L); 
     synchronized(store) { 
      store.setContents(vectorData); 
      store.commit(); 
     } 
    //} 
} 

{ 
    Vector v = new Vector(); 
    loadUserInfo(v); 
    ListEventScreen(v, ...); 
    ... modify v contents as needed ... 
    saveUserInfo(v); 
} 

如果你不介意改變你的持久存儲內容的格式,我想包店在一個單獨的類來代替:

public class EventBeans extends Vector implements Persistable 
{ 
    private static final long persistKey = 0x1dfc10ec9447eb14L; 

    private static EventBeans _instance = null; 
    private static PersistentObject _persist = null; 

    static{ 
     _persist = PersistentStore.getPersistentObject(persistKey); 
     _instance = (EventBeans) _persist.getContents(); 
     if (_instance == null){ 
      _instance = new EventBeans(); 
      _persist.setContents(_instance); 
      _persist.commit(); 
     } 
    } 

    private EventBeans(){  
     super(); 
    } 

    public static EventBeans getInstance(){ 
     return _instance; 
    } 

    public static synchronized void save(){ 
     _persist.commit(); 
    } 
} 

{ 
    Vector v = EventBeans.getInstance(); 
    ListEventScreen(v, ...); 
    ... modify v contents as needed ... 
    EventBeans.save(); 
} 
+0

,我沒有得到什麼時候調用loadUserInfo方法的代碼片段u添加 - { Vector v = new Vector(); loadUserInfo(v); ListEventScreen(v,...); ...根據需要修改v內容... saveUserInfo(v); } 不能理解 – 2012-03-13 05:34:28

+0

如果您想要保存已保存在商店中的事件,則必須從商店中讀取它們並將它們放入Vector中,然後將其保存回商店中。你什麼時候打電話?在您更新商店之前。有什麼不清楚的? – 2012-03-13 06:18:43