WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章手机编程BlackBerryBlackBerry 应用程序开发者指南 第二卷:高级--第8章 存储持久数据

BlackBerry 应用程序开发者指南 第二卷:高级--第8章 存储持久数据
作者:佚名  来源:本站整理  发布时间:2008-3-15 1:42:02

   

       private MenuItem getItem = new MenuItem( _resources.getString(MENUITEM_GET), 110, 11 ) {

           public void run() {

              synchronized(store) {

                  String[] currentinfo = (String[])store.getContents();

                  if(currentinfo == null) {

                     Dialog.alert(_resources.getString(APP_ERROR));

                     }

                  else {

                     currentusernamefield.setText(currentinfo[0]);

                     currentpasswordfield.setText(currentinfo[1]);

                     }

                  }

              }

           };

          

           public static void main(String[] args) {

              UserInfo app = new UserInfo();

              app.enterEventDispatcher();

              }

          

           public UserInfo() {

              MainScreen mainScreen = new MainScreen();

              mainScreen.setTitle(new LabelField(

                     _resources.getString(APPLICATION_TITLE)));

              usernamefield = new AutoTextEditField(

                     _resources.getString(FIELD_NAME), "");

              passwordfield = new PasswordEditField(

                     _resources.getString(FIELD_PASSWORD),"");

              currentusernamefield = new AutoTextEditField(

                     _resources.getString(FIELD_CURRENTNAME), "");

              currentpasswordfield = new AutoTextEditField(

                     _resources.getString(FIELD_CURRENTPASSWORD),"");

              SeparatorField separator = new SeparatorField();

              mainScreen.add(usernamefield);

              mainScreen.add(passwordfield);

              mainScreen.add(separator);

              mainScreen.add(currentusernamefield);

              mainScreen.add(currentpasswordfield);

              mainScreen.addKeyListener(this);

              mainScreen.addTrackwheelListener(this);

              pushScreen(mainScreen);

              }

          

           public void makeMenu( Menu menu, int instance ) {

              menu.add(saveItem);

              menu.add(getItem);

              super.makeMenu(menu, 0);

              }

          

           public void onExit() {

              Dialog.alert(_resources.getString(APP_EXIT));

              }

           }


<!--[if !vml]-->

内存管理和持久化对象

BlackBerry设备上有固定数量的持久化对象句柄以及对象句柄.

闪存

持久化对象句柄

对象句柄

8 MB

12,000

24,000

16 MB

27,000

56,000

32 MB

65,000

132,000

BlackBerry设备上的每个持久化对象都会要求一个持久化对象句柄以及一个对象句柄.例如,一个记录包含了10String字段,那么它将要求11个对象句柄-一个为记录,一个为每个String.如果记录是持久化的,它将需要额外的11个持久化对象句柄.

使用下面的技术限制应用程序需要的持久化对象句柄数:

  • <!--[endif]-->如果可能,使用原始类型代替对象.一个原始类型,例如一个int或者一个char,它们不需要一个对象句柄.
  • 使用对象分组API (net.rim.device.api.system.ObjectGroup)将对象分组.一个分组的对象仅需要一个对象句柄.

:分组的对象是可读的.在做出改变之前调用ObjectGroup.expandGroup()撤销对象的分组.在完成改变之后,调用ObjectGroup.createGroup()将对象分组.必要时仅撤销对象的分组,当撤销对象分组时有性能将受到损耗,因为系统创建了分组对象的一个拷贝,并且将句柄分配给每个组里的对象.

为获取更多信息,参看BlackBerry JDE最佳内存实践白皮书.

管理自定义对象

创建一个数据库

创建一个Vector对象存储多个对象.创建一个PersistentObject作为应用程序的根数据库.

private static Vector _data;

PersistentObject store;

 

static {

    store = PersistentStore.getPersistentObject( 0xdec6a67096f833cL );

    //key is hash of test.samples.restaurants

    _data = (Vector)store.getContents();

    synchronized (store) {

       if (_data == null) {

           _data = new Vector();

           store.setContents(_data);

           store.commit();

           }

       }

    }

持久存储数据

可以持久化实现了Persistable接口的对象.

下面的代码实例作为inner类实现Persistable接口.它定义了一个带有4Object的数组来存储餐厅名,地址,电话号码,以及特色.并且定义了方法来获取和设置Object元素值.

 :一个类为了其对象,必须显式的实现Persistable接口来也就存储. 甚至其子类也适用这个需求.例如类A实现了Persistable接口,并且A有一个子类B,B的对象不能持久存储,除非B也实现Persistable接口.

private static final class RestaurantInfo implements Persistable {

private String[] _elements;

public static final int NAME = 0;

public static final int ADDRESS = 1;

public static final int PHONE = 2;

public static final int SPECIALTY = 3;

public RestaurantInfo() {

    _elements = new String[4];

    for ( int i = 0; i < _elements.length; ++i) {

       _elements[i] = new String("");

       }

}

 

public String getElement(int id) {

    return _elements[id];

}

 

public void setElement(int id, String value) {

    _elements[id] = value;

    }

 

}

创建扩展的对象

下面的方法允许你加入字段到对象中:

  • 在一个int,存储Boolean值做为bit.保留多余的位供将来使用.
  • 直接存储String,但是使用一个键/值对的VectorHashtable,这样附加的(或很少使用的)的字段也可以增加.
  •  如果你由一个表的索引,将他们存储在一个Vector或数组里,这样你可以增加未来的索引了.

保存一个对象

定义一个对象

下面的代码实例创建一个RestaurantInfo对象,并且使用它的设置方法来定义他的组件.

RestaurantInfo info = new RestaurantInfo();

info.setElement(RestaurantInfo.NAME, namefield.getText());

info.setElement(RestaurantInfo.ADDRESS,addressfield.getText());

info.setElement(RestaurantInfo.PHONE, phonefield.getText());

info.setElement(RestaurantInfo.SPECIALTY, specialtyfield.getText());

 

将一个对象加到一个Vector

调用addElement().

_data.addElement(info);

 

上一页  [1] [2] [3] [4] [5]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码