黑莓持久存储问题,

时间:2010-08-16 04:07:00

标签: blackberry storage

我需要在持久存储中存储几个促销。这是班级:

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. 当应用关闭并再次运行时,数据消失了。嗯......不是应该坚持下去吗?

1 个答案:

答案 0 :(得分: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;
    }
}