Blackberry Persistent Storage [closed]
provide a sample code for persistent storage and where can i find the saved data and how to show multiple records from persistent storage to on th BB
Find here the code below to save to persistent store and get the data back:
protected static long infoKey = 0x26a46589530f909aL;
public static Vector getInfo() {
PersistentObject object = PersistentStore.getPersistentObject( infoKey );
myVector table = (myVector) object.getContents();
return table;
}
public static void setInfo(Vector obj) {
try { PersistentStore.destroyPersistentObject(infoKey); } catch (Exception ex) { }
PersistentObject object = PersistentStore.getPersistentObject( infoKey );
object.setContents(obj);
object.commit();
}
This link may help you Using Persistent Store in BlackBerry
public DataContext() {
// Hash of examples.persistentstore.
persistentObject = PersistentStore.getPersistentObject(0xc8027082ac5f496cL);
synchronized(persistentObject) {
settingsTable = (Hashtable)persistentObject.getContents();
if (null == settingsTable) {
settingsTable = new Hashtable();
persistentObject.setContents(settingsTable);
persistentObject.commit();
}
}
}
class HomeScreen extends MainScreen {
private EditField homepageEditField;
private MenuItem saveMenu = new MenuItem("Save", 100, 100) {
public void run() {
Screen screen = UiApplication.getUiApplication().getActiveScreen();
try {
screen.save();
} catch (java.io.IOException ex) {
Dialog.inform("Could not save settings.");
}
screen.close();
}
};
public HomeScreen() {
super();
this.setTitle("Persistent Store Example");
DataContext dataContext = new DataContext();
homepageEditField = new EditField("Home page: ",(String)dataContext.get("HomePage"),256,EditField.FIELD_RIGHT);
this.add(homepageEditField);
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(saveMenu);
}
public void save() throws java.io.IOException {
DataContext dataContext = new DataContext();
dataContext.set("HomePage",homepageEditField.getText().trim());
dataContext.commit();
}
}
精彩评论