Monday, April 11, 2011

Store Data with Persistent API in Blackberry

1. Make Model Class with Persistable interface Implementaion

public class User implements Persistable {
String userName;
String password;
String casinoCode;
boolean saveUserInfo;
public boolean isSaveUserInfo() {
return saveUserInfo;
}
public void setSaveUserInfo(boolean saveUserInfo) {
this.saveUserInfo = saveUserInfo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCasinoCode() {
return casinoCode;
}
public void setCasinoCode(String casinoCode) {
this.casinoCode = casinoCode;
}
}



2. Store object with Persistent API on Load


PersistentObject _store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
synchronized(_store)
{
try {
if(_store.getContents() == null)
{
User user= new User();
user.setCasinoCode("");
user.setPassword("");
user.setUserName("");
user.setSaveUserInfo(false);
_store.setContents(user);
_store.commit();
}

} catch (Exception e) {
PhotoAPP.errorDialog(e.toString());
}
}


3. Save Data with Peristent API

public static void persist(User user)
{
_store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
synchronized( _store )
{
_store.setContents(user);
PersistentObject.commit(_store);
}
}


4. Retrieve data with Persistent API

public static User getPersistentObject() {
_store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
User user = (User)_store.getContents();
return user;
}

No comments:

Post a Comment