0

I am working with persistent store and persistent object.I am being able to save values.I am storing each primitive into a vector and then saving the vector.So what happens now,when i start the app and say save three values.Those values are stored in the vector.Than if i start the app,n check for those values,its displaying the saved values properly.Now if i say again save two more values,than total saved value in the vector should be 5.But when i close the app and restart it.It shows only the last two values which i saved,the earlier three saved value is not displaying.Please help me

package com.kcrw.ui;
import java.util.Random;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import java.util.Vector;

import com.kcrw.model.Song;

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

public class Persist implements Persistable{

    public static PersistentObject abc;
    public static PersistentObject abc1;
    public static PersistentObject abc2;
    public static PersistentObject abc3;
    public static String b;
    public static String c;
    public static String d;
    public static String e;
    public static Vector vect;
    public static Vector xyz=new Vector();
    static {
          abc   = PersistentStore.getPersistentObject(0xb92c8fe20b256b82L);
          abc1   = PersistentStore.getPersistentObject(0xa94f6433aaf45909L);
          abc2   = PersistentStore.getPersistentObject(0xfbe29f690c998fb1L);
          abc3   = PersistentStore.getPersistentObject(0x67a6bd7c03940754L);
    }

    public static void data(){
    synchronized (abc) {

        abc.setContents(((Song) MoreInfoSongDetails.shows.elementAt(MoreInfo.listFieldIndex))
                .getTitle());
        abc.commit();
         }


synchronized (abc1) {

        abc1.setContents(((Song) MoreInfoSongDetails.shows.elementAt(MoreInfo.listFieldIndex))
                .getAirtime());
        abc1.commit();
         }


synchronized (abc2) {

    abc2.setContents(((Song) MoreInfoSongDetails.shows.elementAt(MoreInfo.listFieldIndex))
            .getAlbumImage());
    abc2.commit();
     }
synchronized (abc3) {
      System.out.println("vector size is"+xyz);



    abc3.setContents(xyz);

    abc3.commit();
     }  

}

    public static String getTitle() {
        synchronized (abc) {

            b= (String)abc.getContents();
            //xyz.addElement(b);
            return b;
        }
    }
    public static String getTime() {
        synchronized (abc1) {

            c= (String)abc1.getContents();
            //xyz.addElement(c);
            return c;
        }
    }

    public static String getImage() {
        synchronized (abc2) {

            d= (String)abc2.getContents();
            //xyz.addElement(d);

            return d;
        }

    }
    public static Vector save() {
        synchronized (abc3) {

            vect= (Vector)abc3.getContents();
        int i=vect.size();
        for(int b=0;b<i;b++){
            System.out.println("element at"+b+"is"+vect.elementAt(b));
        }
            return vect;
        }

    }   
}
jmj
  • 237,923
  • 42
  • 401
  • 438
arunabha
  • 17
  • 2
  • I don't see any code that assigns anything to the vector. Am also confused that your "save" function reads the vector from the persistent store and your "data" method stores the vector. – Eric Giguere Feb 02 '11 at 15:01
  • Actually i am really sorry,the methods where i commented out xyz.addElement() in getTitle(0,getImage(),getTime(0 are the places where i am adding elements to the vector xyz.It shouldn't be commented.But the prob still remains what i described above – arunabha Feb 02 '11 at 15:44
  • i think each time i call this class vector xyz is recreated as we are using new vector(),bt if i dont use new vector,i get null pointer exception – arunabha Feb 02 '11 at 15:54

1 Answers1

1

Your code example isn't great, but saving and restoring a vector is quite trivial, as shown here:

http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/PersistentStore.html

I incorporated this into a little tutorial on object persistence. There's also a PowerPoint deck here that gives even more details.

Look through those pages and you should be able to find your problem.\

Eric Giguere
  • 3,495
  • 15
  • 11
  • Thnx for your help.The thing is that i can save and retrive for a single go.But when i close the app and start again,and save new values,then the old saved values goes off,and only the newly saved valued is displayed – arunabha Feb 02 '11 at 16:42
  • 1
    You must read the old vector in whenever your app starts. Otherwise you just end up writing over it. – Eric Giguere Feb 02 '11 at 16:54
  • Here is a SOF example I posted a while back: http://stackoverflow.com/questions/3805182/how-do-i-use-the-persistent-object-store-in-blackberry/3808672#3808672 – eSniff Feb 03 '11 at 07:05