黑莓持久存储

时间:2011-09-16 07:22:05

标签: blackberry

提供持久存储的示例代码,在哪里可以找到已保存的数据以及如何在持久存储中显示多个记录到BB

2 个答案:

答案 0 :(得分:2)

在这里找到以下代码以保存到持久存储并获取数据:

    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();
    }

答案 1 :(得分:1)

此链接可能对您有所帮助 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();

    }
}