本文和大家重点学习一下Blackberry开发平台的永久存储-PersistentStore,主要包括BlackberrySDK5.0版本支持三种存储方式,PersistentStore存储对数据类型和对象的要求等内容,相信通过本文的学习你对Blackberry开发平台的永久存储-PersistentStore的概念一定会有深入的了解。
Blackberry开发平台的永久存储-PersistentStore
BlackberrySDK5.0版本支持三种存储方式:
◆RMS-RecordStore
◆PersistentStore-persistentstoreforRIMlet
◆SQLLiteforRIMlet
RMS方式的存储是基于byte数组的存储,对象需要先序列化,再存储。SQLLite是数据库方式存储,支持SQL语句,这是5.0SDK的新特性。Blackberry最成熟,最流行的存储方式是PersistentStore,可以直接存储对象。本文详细介绍PersistentStore的方式。
BlackBerry开发平台中PersistentStore存储对数据类型和对象的要求:
◆被存储对象只包含基本数据类型-int,String,long,boolean,byte,等等
◆被存储类必须要执行Persistable接口
◆PersistentStore以Key-Valuepair的方式进行数据存储,key必须是一个long值,value的类型是Object.下面来看一个例子:
1.创建存储数据类,比如classMyData:
- publicclassMyDataimplementsPersistable{
- privateStringm_userName;
- privateStringm_company;
- privateStringm_title;
- privateintm_age;
- publicMyData(){}
- publicvoidsetUserName(StringuserName){
- m_userName=userName;
- }
- publicStringgetUserName(){
- returnm_userName;
- }
- publicvoidsetCompany(Stringcompany){
- m_company=company;
- }
- publicStringgetCompany(){
- returnm_company;
- }
- publicvoidsetTitle(Stringtitle){
- m_title=title;
- }
- publicStringgetTitle(){
- returnm_title;
- }
- publicvoidsetAge(intage){
- m_age=age;
- }
- publicintgetAge(){
- returnm_age;
- }
- }
注意的是MyData类执行了Persistable接口,这个接口没有任何要执行的函数。MyData的instancevariable都是基本类型,也可以有方法。
2.存储数据
- publicstaticfinallongID=0x5d8a91784555e0f8L;
- publicvoidsaveData(){
- if(myData==null){
- myData=newMyData();
- }
- myData.setUserName("Protoss");
- myData.setCompany("Blizzard");
- myData.setTitle("Manager");
- myData.setAge(26);
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
- synchronized(dataStore){
- if(dataStore.getContents()==null){
- dataStore.setContents(myData);
- dataStore.commit();
- }
- }
- }
PersistentStore是key-valuepair的存储,需要一个ID,对应一个PersistentObject,存取数据时都要用到,一个ID只对应一个PersistentObject。存储第一步需要从PersistentStore中用ID获取PersistentObject,再调用PersistentObject.setContents(Object)接口填入存储对象,这步其实还没有保存数据,需要调用PersistentObject.commit()保存数据。
3.获取数据
- publicstaticfinallongID=0x5d8a91784555e0f8L;
- publicvoidrecoverDataFromStore(){
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
- synchronized(dataStore){
- if(dataStore.getContents()!=null){
- myData=(MyData)dataStore.getContents();
- }
- }
- if(myData!=null){
- userNameField.setText(myData.getUserName());
- companyField.setText(myData.getCompany());
- titleField.setText(myData.getTitle());
- ageField.setText(myData.getAge()+"");
- }
- }
获取数据第一步用ID获取对应的持久对象PersistentObject,在调用PersistentObject.getContents()获取数据对象,获取的对象类为Object,需要转换成实际的类型,比如myData=(MyData)dataStore.getContents()。
如下是BlackBerry开发平台中PersistentStore的完整例子代码,可以拷贝到Eclipse里运行:
- importnet.rim.device.api.ui.UiApplication;
- publicclassMyAppextendsUiApplication{
- publicstaticvoidmain(String[]args){
- MyAppapp=newMyApp();
- app.enterEventDispatcher();
- }
- publicMyApp(){
- MyScreenmyScreen=newMyScreen();
- pushScreen(myScreen);
- }
- }
- importnet.rim.device.api.util.Persistable;
- publicclassMyDataimplementsPersistable{
- privateStringm_userName;
- privateStringm_company;
- privateStringm_title;
- privateintm_age;
- publicMyData(){}
- publicvoidsetUserName(StringuserName){
- m_userName=userName;
- }
- publicStringgetUserName(){
- returnm_userName;
- }
- publicvoidsetCompany(Stringcompany){
- m_company=company;
- }
- publicStringgetCompany(){
- returnm_company;
- }
- publicvoidsetTitle(Stringtitle){
- m_title=title;
- }
- publicStringgetTitle(){
- returnm_title;
- }
- publicvoidsetAge(intage){
- m_age=age;
- }
- publicintgetAge(){
- returnm_age;
- }
- }
- importnet.rim.device.api.system.PersistentObject;
- importnet.rim.device.api.system.PersistentStore;
- importnet.rim.device.api.ui.Field;
- importnet.rim.device.api.ui.FieldChangeListener;
- importnet.rim.device.api.ui.component.BasicEditField;
- importnet.rim.device.api.ui.component.ButtonField;
- importnet.rim.device.api.ui.component.LabelField;
- importnet.rim.device.api.ui.container.MainScreen;
- publicclassMyScreenextendsMainScreenimplementsFieldChangeListener{
- publicstaticfinallongID=0x5d8a91784555e0f8L;
- privatePersistentObjectdataStore;
- privateBasicEditFielduserNameField;
- privateBasicEditFieldcompanyField;
- privateBasicEditFieldtitleField;
- privateBasicEditFieldageField;
- privateButtonFieldsaveButton;
- privateMyDatamyData;
- publicMyScreen(){
- super();
- setTitle("PersistDataTest");
- initialize();
- addToWindow();
- recoverDataFromStore();
- }
- privatevoidinitialize(){
- userNameField=newBasicEditField("UserName:","",20,BasicEditField.FILTER_DEFAULT);
- companyField=newBasicEditField("CompanyName:","",20,BasicEditField.FILTER_DEFAULT);
- titleField=newBasicEditField("Title:","",20,BasicEditField.FILTER_DEFAULT);
- ageField=newBasicEditField("Age:","",20,BasicEditField.FILTER_NUMERIC);
- saveButton=newButtonField("save");
- saveButton.setChangeListener(this);
- }
- privatevoidaddToWindow(){
- add(userNameField);
- add(titleField);
- add(companyField);
- add(ageField);
- add(saveButton);
- }
- publicvoidsaveData(){
- if(myData==null){
- myData=newMyData();
- }
- myData.setUserName(userNameField.getText());
- myData.setCompany(companyField.getText());
- myData.setTitle(titleField.getText());
- myData.setAge(Integer.valueOf(ageField.getText()).intValue());
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
- synchronized(dataStore){
- if(dataStore.getContents()==null){
- dataStore.setContents(myData);
- dataStore.commit();
- }
- }
- }
- publicvoidrecoverDataFromStore(){
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
- synchronized(dataStore){
- if(dataStore.getContents()!=null){
- myData=(MyData)dataStore.getContents();
- }
- }
- if(myData!=null){
- userNameField.setText(myData.getUserName());
- companyField.setText(myData.getCompany());
- titleField.setText(myData.getTitle());
- ageField.setText(myData.getAge()+"");
- }
- }
- publicvoidfieldChanged(Fieldfield,intcontext){
- if(field==saveButton){
- saveData();
- }
- }
- }