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

保存一个更新的对象

调用PersistentObjectsetContents(),然后调用commit()方法保存一个更新的对象

synchronized(store) {

    store.setContents(_data);

    store.commit();

}

: 当你做出改变时,同步一个持久对象,这样其他的线程在同一时间就不能做出改变.

获取一个对象

为了获取最近保存的对象,调用_data.lastElement().

public void run() {

synchronized(store) {

    _data = (Vector)store.getContents();

    if (!_data.isEmpty()) { RestaurantInfo info = (RestaurantInfo)_data.lastElement();

    namefield.setText(info.getElement(RestaurantInfo.NAME));

    addressfield.setText(info.getElement(RestaurantInfo.ADDRESS));

    phonefield.setText(info.getElement(RestaurantInfo.PHONE));

    specialtyfield.setText(info.getElement(

           RestaurantInfo.SPECIALTY));

     }

    }

}

代码实例

本实例描述了如何创建一个应用程序,它允许用户存储一个关于喜爱的餐厅信息.

本实例也允许用户保存多个餐厅的信息,并且可以查看最新保存的餐厅信息.


 

: Restaurants.java

/* Restaurants.java

* Copyright (C) 2004-2005 Research In Motion Limited.

*/

package com.rim.samples.docs.restaurants;

import net.rim.device.api.ui.*;

import net.rim.device.api.ui.component.*;

import net.rim.device.api.ui.container.*;

import net.rim.device.api.system.*;

import net.rim.device.api.util.*;

import java.util.*;

import net.rim.device.api.i18n.*;

import net.rim.blackberry.api.invoke.*;

import net.rim.blackberry.api.browser.*;

import com.rim.samples.docs.baseapp.*;

import com.rim.samples.docs.resource.*;

 

public class Restaurants extends BaseApp

             implements RestaurantResource,KeyListener, TrackwheelListener {

    private AutoTextEditField namefield;

    private AutoTextEditField addressfield;

    private EditField phonefield;

    private EditField websitefield;

    private EditField specialtyfield;

    private static Vector _data;

    private static PersistentObject store;

    private static ResourceBundle _resources;

    private MenuItem saveItem = new MenuItem(_resources.getString(MENUITEM_SAVE), 110, 10) {

       public void run() {

           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.WEBSITE, phonefield.getText());

           info.setElement(RestaurantInfo.SPECIALTY,

                  specialtyfield.getText());

           _data.addElement(info);

           synchronized(store) {

              store.setContents(_data);

              store.commit();

              }

           Dialog.inform(_resources.getString(APP_SUCCESS));

           namefield.setText(null);

           addressfield.setText(null);

           phonefield.setText("");

           websitefield.setText("");

           specialtyfield.setText("");

           }

       };

      

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

           public void run() {

              synchronized(store) {

                  _data = (Vector)store.getContents();

                  if (!_data.isEmpty()) {

                     RestaurantInfo info = (RestaurantInfo)_data.lastElement();

                     namefield.setText(info.getElement(RestaurantInfo.NAME));

                     addressfield.setText(info.getElement(RestaurantInfo.ADDRESS));

                     phonefield.setText(info.getElement(RestaurantInfo.PHONE));

                     websitefield.setText(info.getElement(RestaurantInfo.WEBSITE));

                     specialtyfield.setText(info.getElement(RestaurantInfo.SPECIALTY));

                     }

                  }

              }

           };

          

           private MenuItem phoneItem = new MenuItem(_resources.getString(MENUITEM_PHONE), 110, 12) {

              public void run() {

                  synchronized(store) {

                     String phoneNumber = phonefield.getText();

                     if ( phoneNumber.length() == 0) {

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

                         }

                     else {

                         PhoneArguments call =

                            new PhoneArguments(PhoneArguments.ARG_CALL, phoneNumber);

                         Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, call);

                         }

                     }

                  }

              };

             

              private MenuItem browserItem =

                  new MenuItem(_resources.getString(MENUITEM_BROWSER), 110, 12) {

                  public void run() {

                     synchronized(store) {

                         String websiteUrl = websitefield.getText();

                         if (websiteUrl.length() == 0) {

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

                            }

                         else {

                            BrowserSession visit = Browser.getDefaultSession();

                            visit.displayPage(websiteUrl);

                            }

                         }

                     }

                  };

                 

                  static {

                     _resources = ResourceBundle.getBundle(

                            RestaurantResource.BUNDLE_ID,

                            RestaurantResource.BUNDLE_NAME);

                     store = PersistentStore.getPersistentObject(0xdec6a67096f833cL);

                    

                     // Key is hash of test.samples.restaurants.

                     synchronized (store) {

                         _data = (Vector)store.getContents();

                         if (_data == null) {

                            _data = new Vector();

                            store.setContents( _data );

                            store.commit();

                            }

                         }

                     }

                  public static void main(String[] args) {

                     Restaurants app = new Restaurants();

                     app.enterEventDispatcher();

                     }

 

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

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

用户名: 查看更多评论

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

内 容:

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