每次調用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();
}
,我沒有得到什麼時候調用loadUserInfo方法的代碼片段u添加 - { Vector v = new Vector(); loadUserInfo(v); ListEventScreen(v,...); ...根據需要修改v內容... saveUserInfo(v); } 不能理解 – 2012-03-13 05:34:28
如果您想要保存已保存在商店中的事件,則必須從商店中讀取它們並將它們放入Vector中,然後將其保存回商店中。你什麼時候打電話?在您更新商店之前。有什麼不清楚的? – 2012-03-13 06:18:43