BlackBerry开发平台的永久存储-Persistent Store

移动开发
BlackBerry开发的概念你是否了解,这里和大家分享一下Blackberry开发平台的永久存储-PersistentStore,希望本文的介绍能让你有所收获。

本文和大家重点学习一下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:

 

  1. publicclassMyDataimplementsPersistable{  
  2.  
  3. privateStringm_userName;  
  4. privateStringm_company;  
  5. privateStringm_title;  
  6. privateintm_age;  
  7.  
  8. publicMyData(){}  
  9.  
  10. publicvoidsetUserName(StringuserName){  
  11. m_userName=userName;  
  12. }  
  13.  
  14. publicStringgetUserName(){  
  15. returnm_userName;  
  16. }  
  17.  
  18. publicvoidsetCompany(Stringcompany){  
  19. m_company=company;  
  20. }  
  21.  
  22. publicStringgetCompany(){  
  23. returnm_company;  
  24. }  
  25.  
  26. publicvoidsetTitle(Stringtitle){  
  27. m_title=title;  
  28. }  
  29. publicStringgetTitle(){  
  30. returnm_title;  
  31. }  
  32. publicvoidsetAge(intage){  
  33. m_age=age;  
  34. }  
  35. publicintgetAge(){  
  36. returnm_age;  
  37. }  
  38. }  
  39.  

 

注意的是MyData类执行了Persistable接口,这个接口没有任何要执行的函数。MyData的instancevariable都是基本类型,也可以有方法。

2.存储数据

 

  1. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  2.  
  3. publicvoidsaveData(){  
  4.  
  5. if(myData==null){  
  6. myData=newMyData();  
  7. }  
  8.  
  9. myData.setUserName("Protoss");  
  10. myData.setCompany("Blizzard");  
  11. myData.setTitle("Manager");  
  12. myData.setAge(26);  
  13.  
  14. if(dataStore==null){  
  15. dataStore=PersistentStore.getPersistentObject(ID);  
  16. }  
  17.  
  18. synchronized(dataStore){  
  19. if(dataStore.getContents()==null){  
  20. dataStore.setContents(myData);  
  21. dataStore.commit();  
  22. }  
  23. }  
  24. }  
  25.  

 

PersistentStore是key-valuepair的存储,需要一个ID,对应一个PersistentObject,存取数据时都要用到,一个ID只对应一个PersistentObject。存储第一步需要从PersistentStore中用ID获取PersistentObject,再调用PersistentObject.setContents(Object)接口填入存储对象,这步其实还没有保存数据,需要调用PersistentObject.commit()保存数据。

3.获取数据

 

  1. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  2.  
  3. publicvoidrecoverDataFromStore(){  
  4.  
  5. if(dataStore==null){  
  6. dataStore=PersistentStore.getPersistentObject(ID);  
  7. }  
  8.  
  9. synchronized(dataStore){  
  10.  
  11. if(dataStore.getContents()!=null){  
  12. myData=(MyData)dataStore.getContents();  
  13. }  
  14. }  
  15.  
  16. if(myData!=null){  
  17. userNameField.setText(myData.getUserName());  
  18. companyField.setText(myData.getCompany());  
  19. titleField.setText(myData.getTitle());  
  20. ageField.setText(myData.getAge()+"");  
  21. }  
  22. }  
  23.  

 

获取数据第一步用ID获取对应的持久对象PersistentObject,在调用PersistentObject.getContents()获取数据对象,获取的对象类为Object,需要转换成实际的类型,比如myData=(MyData)dataStore.getContents()。

如下是BlackBerry开发平台中PersistentStore的完整例子代码,可以拷贝到Eclipse里运行:

 

 

  1. importnet.rim.device.api.ui.UiApplication;  
  2. publicclassMyAppextendsUiApplication{  
  3. publicstaticvoidmain(String[]args){  
  4. MyAppapp=newMyApp();  
  5. app.enterEventDispatcher();  
  6. }  
  7.  
  8. publicMyApp(){  
  9. MyScreenmyScreen=newMyScreen();  
  10. pushScreen(myScreen);  
  11. }  
  12. }  
  13.  
  14. importnet.rim.device.api.util.Persistable;  
  15. publicclassMyDataimplementsPersistable{  
  16.  
  17. privateStringm_userName;  
  18. privateStringm_company;  
  19. privateStringm_title;  
  20. privateintm_age;  
  21.  
  22. publicMyData(){}  
  23.  
  24. publicvoidsetUserName(StringuserName){  
  25. m_userName=userName;  
  26. }  
  27.  
  28. publicStringgetUserName(){  
  29. returnm_userName;  
  30. }  
  31.  
  32. publicvoidsetCompany(Stringcompany){  
  33. m_company=company;  
  34. }  
  35.  
  36. publicStringgetCompany(){  
  37. returnm_company;  
  38. }  
  39.  
  40. publicvoidsetTitle(Stringtitle){  
  41. m_title=title;  
  42. }  
  43.  
  44. publicStringgetTitle(){  
  45. returnm_title;  
  46. }  
  47.  
  48. publicvoidsetAge(intage){  
  49. m_age=age;  
  50. }  
  51.  
  52. publicintgetAge(){  
  53. returnm_age;  
  54. }  
  55. }  
  56.  
  57. importnet.rim.device.api.system.PersistentObject;  
  58. importnet.rim.device.api.system.PersistentStore;  
  59. importnet.rim.device.api.ui.Field;  
  60. importnet.rim.device.api.ui.FieldChangeListener;  
  61. importnet.rim.device.api.ui.component.BasicEditField;  
  62. importnet.rim.device.api.ui.component.ButtonField;  
  63. importnet.rim.device.api.ui.component.LabelField;  
  64. importnet.rim.device.api.ui.container.MainScreen;  
  65.  
  66. publicclassMyScreenextendsMainScreenimplementsFieldChangeListener{  
  67.  
  68. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  69.  
  70. privatePersistentObjectdataStore;  
  71.  
  72. privateBasicEditFielduserNameField;  
  73. privateBasicEditFieldcompanyField;  
  74. privateBasicEditFieldtitleField;  
  75. privateBasicEditFieldageField;  
  76. privateButtonFieldsaveButton;  
  77.  
  78. privateMyDatamyData;  
  79.  
  80. publicMyScreen(){  
  81. super();  
  82. setTitle("PersistDataTest");  
  83. initialize();  
  84. addToWindow();  
  85. recoverDataFromStore();  
  86. }  
  87.  
  88. privatevoidinitialize(){  
  89. userNameField=newBasicEditField("UserName:","",20,BasicEditField.FILTER_DEFAULT);  
  90. companyField=newBasicEditField("CompanyName:","",20,BasicEditField.FILTER_DEFAULT);  
  91. titleField=newBasicEditField("Title:","",20,BasicEditField.FILTER_DEFAULT);  
  92. ageField=newBasicEditField("Age:","",20,BasicEditField.FILTER_NUMERIC);  
  93. saveButton=newButtonField("save");  
  94. saveButton.setChangeListener(this);  
  95. }  
  96.  
  97. privatevoidaddToWindow(){  
  98. add(userNameField);  
  99. add(titleField);  
  100. add(companyField);  
  101. add(ageField);  
  102. add(saveButton);  
  103. }  
  104.  
  105. publicvoidsaveData(){  
  106.  
  107. if(myData==null){  
  108. myData=newMyData();  
  109. }  
  110.  
  111. myData.setUserName(userNameField.getText());  
  112. myData.setCompany(companyField.getText());  
  113. myData.setTitle(titleField.getText());  
  114. myData.setAge(Integer.valueOf(ageField.getText()).intValue());  
  115.  
  116. if(dataStore==null){  
  117. dataStore=PersistentStore.getPersistentObject(ID);  
  118. }  
  119.  
  120. synchronized(dataStore){  
  121. if(dataStore.getContents()==null){  
  122. dataStore.setContents(myData);  
  123. dataStore.commit();  
  124. }  
  125. }  
  126. }  
  127.  
  128. publicvoidrecoverDataFromStore(){  
  129.  
  130. if(dataStore==null){  
  131. dataStore=PersistentStore.getPersistentObject(ID);  
  132. }  
  133.  
  134. synchronized(dataStore){  
  135.  
  136. if(dataStore.getContents()!=null){  
  137. myData=(MyData)dataStore.getContents();  
  138. }  
  139. }  
  140.  
  141. if(myData!=null){  
  142. userNameField.setText(myData.getUserName());  
  143. companyField.setText(myData.getCompany());  
  144. titleField.setText(myData.getTitle());  
  145. ageField.setText(myData.getAge()+"");  
  146. }  
  147. }  
  148.  
  149. publicvoidfieldChanged(Fieldfield,intcontext){  
  150. if(field==saveButton){  
  151. saveData();  
  152. }  
  153. }  
  154. }  

 

责任编辑:佚名 来源: csdn.net
相关推荐

2018-07-19 10:56:16

Kubernetes存储架构

2018-06-21 15:14:51

Kubernetes存储容器

2013-05-03 11:01:22

iOS开发归档永久存储数据

2009-07-17 14:51:22

.Net Micro

2010-06-22 16:19:01

Black Berry

2010-05-22 16:57:09

BlackBerry开

2011-05-03 15:13:23

BlackBerryWidget

2017-11-21 14:32:05

容器持久存储

2011-07-18 14:46:56

PhoneGapBlackBerry

2010-06-08 16:47:18

BlackBerry开

2011-05-03 16:41:04

BlackBerry

2011-05-03 15:28:15

BlackBerryWidget

2010-08-20 13:47:11

BlackBerry应BlackBerry

2020-03-25 11:37:17

存储云原生DevOps

2011-01-27 17:33:10

DalvikAndroidQNX

2010-06-12 16:29:00

BlackBerry开

2009-04-09 13:58:58

JavaXML存储

2010-04-09 17:53:56

Black Berry

2010-07-22 15:22:58

BlackBerry开

2010-04-02 17:45:22

Black Berry
点赞
收藏

51CTO技术栈公众号