views:

76

answers:

1

Hi,

I need to store several promos in the persistant storage. Here's the class:

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;
    }

}

And this is how I put it into the persistant storage:

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;
    }

My two problems are:

  1. Only the first promo actually stored in the persistant storage. The rest are just the same.

  2. When the app is closed and run again, the data is gone. Hmm... isn't it should be persistent?

+2  A: 

Your properties are all static, they should't be.

You should use public getter and setter to access your data in a class instance.

You should probably read the tutorial on 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;
    }
}
Michael B.
private static Vector favoritTable = new Vector();now the promos are added correctlybut if the emulator is restarted, and the promos will be gone.i thought it should be persistant?
anta40
@anta40, On my side, they are still there.
Michael B.