Search code examples
eclipseblackberrydata-persistence

Persistent object in BlackBerry Application


I have been trying to use the persistent object in BlackBerry Application. I am making a login page where I can save the username and password in the application. Below is the code I used in the login page

public final class LoginScreen extends MainScreen
{
    private BasicEditField useremailField;
    private PasswordEditField passwordField;
    public HorizontalFieldManager hfm = new HorizontalFieldManager (FIELD_VCENTER);


    public Hashtable persistentHashtable;
    
    public PersistentObject persistentObject;
    
    public static final long KEY = 0x9df9f961bc6d6baL;

    public LoginScreen()
    {        
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        // Set the displayed title of the screen       
        setTitle(new LabelField("Log In",  LabelField.HCENTER| LabelField.USE_ALL_WIDTH));
    
        persistentObject = PersistentStore.getPersistentObject(KEY); 
        
        useremailField = new BasicEditField( "Email Address:", "", 100, BasicEditField.EDITABLE );
        passwordField = new PasswordEditField( "Password: ", "", 100, PasswordEditField.EDITABLE);
        
        ButtonField registerField = new ButtonField( "Register", ButtonField.CONSUME_CLICK | ButtonField.FIELD_LEFT);
        ButtonField loginField = new ButtonField( "Login", ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT);
                
        
        add(new LabelField("\n\n"));
        add( useremailField );
        add(new LabelField("\n"));
        add( passwordField);
        add(new LabelField("\n"));
       
        hfm.add(registerField);
        hfm.add(loginField);
     
        setStatus(hfm);
     
        loginField.setChangeListener( new FieldChangeListener() 
        {
            public void fieldChanged( Field arg0, int arg1 )
            {
                login();
            }
        } );
        persistentHashtable.put("UsernameData", useremailField);
        persistentHashtable.put("PasswordData", passwordField);
        
        persistentObject.commit();
        
    }
    private void login() {
        UiApplication ui = UiApplication.getUiApplication();
        ui.pushScreen(new Loggedin());
            
    }
}

Below is the code of the logged in page

public class Loggedin extends MainScreen
{
    private LabelField username;
    private LabelField password;
    
    public static PersistentObject persistentObject;
    public Loggedin()
    {
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        setTitle( "Registration Form" );
         
        if (persistentObject.getContents() == null) {
            persistentHashtable = new Hashtable();
            persistentObject.setContents(persistentHashtable);
        }
        else {
            persistentHashtable = (Hashtable)persistentObject.getContents();
        }
        
        if (persistentHashtable.containsKey("EditData")) {
            username.setText((String)persistentHashtable.get("UsernameData"));
        }
        if (persistentHashtable.containsKey("BoolData")) {
            password.setText((String)persistentHashtable.get("PasswordData"));
        }
    }

}

I haven't been able to rectify the problem.


Solution

  • Please go through the following sample.

    http://www.blackberryforums.com/developer-forum/13335-data-save-read-example.html

    It is better to create a utility class to use the persistent object.

    How To Save BlackBerry Settings in The Persistent Store