2010-08-16 173 views
1

我需要在持久存儲中存儲幾個promos。下面是類:BlackBerry持久存儲問題,

import net.rim.device.api.util.Persistable; 

public class FavoritePromo implements Persistable{ 
    public static String id; 
    public static String merchantName; 
    public static String title; 
    public static String expireDate; 
    public static String couponsLeft; 

    public FavoritePromo(String id, String merchantName, String title, String expireDate, String couponsLeft){ 
     this.id = id; 
     this.merchantName = merchantName; 
     this.title = title; 
     this.expireDate = expireDate; 
     this.couponsLeft = couponsLeft; 
    } 

} 

這是我怎麼把它變成了持續性存儲:

private static final long PERSISTANCE_KEY_2 = 0xb13951b27c2c948aL; 
    public static Vector favoritTable = new Vector(); 
    private static PersistentObject persistentObject2 = PersistentStore.getPersistentObject(PERSISTANCE_KEY_2); 

    public static void addToFavorites(FavoritePromo promo){ 

     synchronized (persistentObject2) { 
      favoritTable.addElement(promo); 
      persistentObject2.setContents(favoritTable); 
      persistentObject2.commit(); 
     } 
    } 

    public static void loadFavorites(){ 
     PersistentObject persistentObject2 = PersistentStore.getPersistentObject(PERSISTANCE_KEY_2); 
     synchronized (persistentObject2) { 
      favoritTable = (Vector) persistentObject2.getContents(); 
      System.out.println("UserData: Retrieve from RMS success"); 
     } 
    } 

    public static void deleteFavorites(){ 
     PersistentStore.destroyPersistentObject(PERSISTANCE_KEY_2); 
    } 

    public static boolean isFavorite(String _id){ 
     FavoritePromo tmp; 
     for (int x = 0; x < favoritTable.size(); x++){ 
      tmp = (FavoritePromo) favoritTable.elementAt(x); 
      if (tmp.id.equals(_id)) { 
       return true; 
      } 
     } 
     return false; 
    } 

我的兩個問題是:

  1. 只有第一個宣傳片實際存儲在持久存儲。其餘的都是一樣的。

  2. 當應用程序關閉並再次運行時,數據消失。嗯...不是應該持久嗎?

回答

3

你的屬性都是靜態的,它們不應該是。

您應該使用公共getter和setter來訪問類實例中的數據。

你或許應該閱讀Class

import java.util.Vector; 

import net.rim.device.api.system.PersistentObject; 
import net.rim.device.api.system.PersistentStore; 

public class PersistantStoreController { 

    private static final long PERSISTANCE_KEY_2 = 0xb13951b27c2c948aL; 

    private Vector favoritTable = new Vector(); 

    public void addToFavorites(FavoritePromo promo) { 
     synchronized (persistentObject2) { 
      if (favoritTable == null) { 
       favoritTable = new Vector(); 
      } 
      favoritTable.addElement(promo); 
      persistentObject2.setContents(favoritTable); 
      persistentObject2.commit(); 
     } 
    } 

    private PersistentObject persistentObject2 = PersistentStore 
      .getPersistentObject(PERSISTANCE_KEY_2); 

    public Vector getFavoriteTable() { 
     return favoritTable; 
    } 

    public void loadFavorites() { 
     PersistentObject persistentObject2 = PersistentStore 
       .getPersistentObject(PERSISTANCE_KEY_2); 
     synchronized (persistentObject2) { 
      favoritTable = (Vector) persistentObject2.getContents(); 
      System.out.println("UserData: Retrieve from RMS success"); 
     } 
    } 

    public void deleteFavorites() { 
     PersistentStore.destroyPersistentObject(PERSISTANCE_KEY_2); 
    } 

    public boolean isFavorite(String _id) { 
     FavoritePromo tmp; 
     for (int x = 0; x < favoritTable.size(); x++) { 
      tmp = (FavoritePromo) favoritTable.elementAt(x); 
      if (tmp.getId().equals(_id)) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 



import net.rim.device.api.util.Persistable; 

public class FavoritePromo implements Persistable { 
    private String id; 
    private String merchantName; 
    private String title; 
    private String expireDate; 
    private String couponsLeft; 

    public FavoritePromo(String id, String merchantName, String title, 
      String expireDate, String couponsLeft) { 
     this.id = id; 
     this.merchantName = merchantName; 
     this.title = title; 
     this.expireDate = expireDate; 
     this.couponsLeft = couponsLeft; 
    } 

    public String getId() { 
     return id; 
    } 
} 
+0

私有靜態矢量favoritTable =新的向量()的教程; 現在這些宣傳片被正確添加了 但是如果模擬器重新啓動了,宣傳片將會消失。 我認爲它應該持續? – anta40 2010-08-18 05:04:12

+0

@ anta40,在我身邊,他們仍然在那裏。 – 2010-08-18 11:49:13