我在黑莓中从持久性存储中获取的哈希表中添加一些数据,用例就像每个日期我得到一些数据将它添加到持久存储中。现在我已经在持久存储中有一些数据用于日期,我需要删除/更新它并添加该日期的新数据,但问题是数据被附加到同一日期而不删除该日期的旧数据。
public Hashtable getUserInfo() {
Hashtable eventBean = null;
store = PersistentStore.getPersistentObject(0xeb263c815d29213aL);
synchronized (store) {
eventBean = (Hashtable) store.getContents();
if (eventBean == null) {
eventBean = new Hashtable();
}
}
return eventBean;
}
private Hashtable getEvents(String usrDate, String timezone) {
listUserEvents = getUserInfo();
listEvents = new Vector();
Enumeration events = eventList.items();
while (events.hasMoreElements()) {
System.out.println("in while");
Event event = (Event) events.nextElement();
if (eventList.isSupportedField(Event.START) &&
event.countValues(Event.START) > 0)
{
long start = event.getDate(Event.START, 0);
SimpleDateFormat sdf =
new SimpleDateFormat("MMM dd,yyyy HH:mm");
String dateString = sdf.formatLocal(start);
SimpleDateFormat sdf1 =
new SimpleDateFormat("MMM dd,yyyy");
String date = sdf1.formatLocal(start);
System.out.println("dates are :" +date+ "user" +usrDate);
if (usrDate.equalsIgnoreCase(date)) {
System.out.println("dates are equal:" +date);
EventsBean eventBean = new EventsBean();
if (eventList.isSupportedField(Event.END) &&
event.countValues(Event.END) > 0)
{
long end = event.getDate(Event.END, 0);
SimpleDateFormat sdform =
new SimpleDateFormat("MMM dd, yyyy HH:mm");
String dateString1 = sdform.formatLocal(end);
eventBean.setStartDate(dateString);
eventBean.setEndDate(dateString1);
}
listEvents.addElement(eventBean);
System.out.println("the size bf hash is" + listEvents.size());
// changed it
if (listUserEvents.containsKey(usrDate)) {
Vector info = (Vector) listUserEvents.get(usrDate);
System.out.println("the size in getEvents is" +
info.size());
info.addElement(eventBean);
System.out.println("vector size info is" + info.size());
listUserEvents.put(usrDate, info);
} else {
listUserEvents.put(usrDate, listEvents);
}
} else {
// Dialog.alert("not equal");
}
}
}
}
答案 0 :(得分:1)
您只需清除日期存储的矢量中的旧数据:
// changed it
if (listUserEvents.containsKey(usrDate)) {
Vector info = (Vector) listUserEvents.get(usrDate);
System.out.println("the size in getEvents is" +
info.size());
info.removeAllElements(); // <=== clears the old data
info.addElement(eventBean);
System.out.println("vector size info is" + info.size());
listUserEvents.put(usrDate, info);
} else {
listUserEvents.put(usrDate, listEvents);
}
答案 1 :(得分:0)
使用以下代码从位置i的矢量中删除元素。
vector.removeElementAt(i);
然后将此向量添加到持久性存储中。每次更新矢量并将矢量添加到商店。