سؤال

I am trying to store the registration state of the user in my app. i have used Persistent store. But i am not getting the data bak from the store. I have written the below code:

package com.saiservices.util;

import java.util.Enumeration;

import com.saiservices.ui.Logger;


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

public class SaiPersistentStore implements Persistable{

    private SaiHashTable mElements = null;
    static SaiPersistentStore instance;
    private static final long KEY = xxxxxxxxxxxx;
    private PersistentObject persistentObject = null;


    /**
     * Gets the single instance of DataBase.
     * 
     * @return single instance of DataBase
     */
    public static SaiPersistentStore getInstance() {
        try {
            if (instance == null) {
                instance = new SaiPersistentStore();
            }
        } catch (Exception e) {
            instance = null;
        }
        return instance;
    }


    private SaiPersistentStore()
    {
         Logger.out("PersistentStoreInfo", "inside constructor  ");
        persistentObject = PersistentStore.getPersistentObject(KEY);
         synchronized (persistentObject) 
           { 
               if (persistentObject.getContents() == null) 
               { 
                   persistentObject.setContents(new SaiHashTable()); 
                   persistentObject.commit(); 
               }  
           }

         mElements = (SaiHashTable)persistentObject.getContents();
         if (mElements == null){
             Logger.out("PersistentStoreInfo", "************"+(mElements == null));
             mElements = new SaiHashTable();
         }
    }


    public String getElement(String key)
    {
        Logger.out("PersistentStoreInfo", "getElement :"+key);
        Logger.out("PersistentStoreInfo", "getElement value   :"+mElements.get(key));

        return (String) mElements.get(key);
    }

    public void setElement(String key, String value)
    {
        Logger.out("PersistentStoreInfo", "setElement11111 :"+key + "   "+value);
        mElements.put(key, value);
        persistentObject.setContents(mElements);
        Logger.out("PersistentStoreInfo", "setElement22222 :"+key+"   "+value);
        persistentObject.commit(); 
    }

    public Enumeration getAllKeys() 
    {
        return mElements.keys();
    }

}

And here is the hashtable code:

package com.saiservices.util;

import java.util.Hashtable;

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

public class SaiHashTable extends Hashtable implements Persistable{

    public SaiHashTable(){
        super();
    }
    public SaiHashTable(int initialCepacity){
        super(initialCepacity);
    }

}

Now i am setting the element like this:

SaiPersistentStore.getInstance().setElement("Registration","on");

But when i am trying to get the element in theis way, gettting null value:

 SaiPersistentStore.getInstance().getElement("Register")

Where i am doing wrong here? Please help.

هل كانت مفيدة؟

المحلول

Remove the following line from setElement() method:

    persistentObject.setContents(mElements);

by doing this, you are overwriting the contents of the persitent object, which were earlier a SaiHashTable object. So next time you try to cast the contents of the persistent object, you get an exception which you catch and then reset the instance.

Also you should try to get the same value you have set:

SaiPersistentStore.getInstance().getElement("Registration")

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top