Tuesday, August 16, 2011

How to use PersistentStore to store objects in BlackBerry Java programming

Here in this post we will see how to store customer data using PersistentStore.
In my DataObject class used to store customer data. it has 2 parameters customer ID and some string property it may be name address or any other parameter. Also you can change those parameters according to your requirements. we are suing vector to store those pairs in runtime. Hope this will useful for you
============================================
package com.DataStore;

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

public final class DataObject implements Persistable
{

    private Vector elements;
    public static final int CLIENT_ID = 0;

    public DataObject()
    {
    elements = new Vector(1);
    int capacity = elements.capacity();
    for (int i = 0; i < capacity; ++i)
    {
        elements.addElement(new String(""));
    }
    }

    /**
     * @param id
     * @return String
     * String getElement(int id)
     */
    public String getElement(int id)
    {
    return (String) elements.elementAt(id);
    }

    public void setElement(int id, String value)
    {
    elements.setElementAt(value, id);
    }
}

==============================================
package com.DataStore;

import java.util.Vector;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
public class DataHandler {

    private static Vector data;
    private static PersistentObject store;

    static
    {
    store =    PersistentStore.getPersistentObject(1);

    synchronized (store) {
        if (store.getContents() == null) {
        store.setContents(new Vector());
        store.commit();
        }
    }

    data = (Vector) store.getContents();

    }

    /**
     * @param info
     * static void saveObject(DataObject info)
     */
    public static void saveObject(DataObject info)
    {
    data.addElement(info);
    synchronized (store) {
        store.setContents(data);
        store.commit();
    }
    }

    /**
     * @return String
     * static String getClient()
     */
    public static String getClient() {

    synchronized (store) {
        data = (Vector) store.getContents();
        if (!data.isEmpty()) {
        DataObject info = (DataObject) data.lastElement();
        return info.getElement(DataObject.CLIENT_ID);
        }
        else
        {
        //If data is empty here we return empty string
        return "";
        }
    }
    }
}

3 comments:

  1. Dear sir,i am new to blackberry. i am trying to remember user name and password by using persistent store.in my application there is a EditField(username),PasswordField(Password)and a checkBoxField(Checkbox).
    and my requirememt is to remember the both when i click remember me check box. i tried so many time but didn't get the result.please give me the full code to do this...so that i will thankful to you all......

    ReplyDelete
  2. Did you try this
    http://sanjeewamalalgoda.blogspot.com/2011/08/how-to-use-persistentstore-to-store.html

    ReplyDelete
  3. upto how much we can store in persistent obejct?

    ReplyDelete