黑莓持久存储区 [英] BlackBerry Persistent store

查看:129
本文介绍了黑莓持久存储区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的,我想使用持久性存储来存储许多数据项的应用程序。问题是每当新条目是由它取​​代了现有条目。
这里是我的code,请帮助我。

 导入net.rim.device.api.ui。*;
导入net.rim.device.api.ui.component。*;
导入net.rim.device.api.ui.container *。
net.rim.device.api.system数据导入*。
导入net.rim.device.api.util *。
进口的java.util。*;
/ *在用户输入数据的应用程序。显示此数据时,用户preSS保存按钮* /
公共类显示扩展的UIApplication {
/ *声明字符串存储用户*的数据/
 字符串的getFirstName;
 串getLastName;
 串getEmail;
 串getGender;
 的getStatus串;
 / *声明文本字段用户输入* /
 私人AutoTextEditField的firstName; 私人AutoTextEditField lastName的; 私人EmailAddressEditField电子邮件;
/ *声明选择字段用户输入* /
 私人ObjectChoiceField性别;
 / *声明复选框栏供用户输入* /
 私人CheckBoxField字段的状态;
 //声明按钮领域
 私人ButtonField字段保存;
 关闭私人ButtonField字段; / *声明向量* /
 私有静态矢量_data;
 / *声明持久化对象* /
 私有静态持久性对象存储; / *创建一个切入点* /
公共静态无效的主要(字串[] args)
{
 / *创建类的实例* /
 显示应用=新显示();
 app.enterEventDispatcher();
}/ *创建默认构造函数* /
 公共显示()
 {
  / *创建主屏幕类​​的一个目的是利用其功能* /
  MainScreen mainScreen =新MainScreen();  //主屏设置标题
  mainScreen.setTitle(新的LabelField(请输入您的数据));  //创建用户输入文本字段
  的firstName =新AutoTextEditField(名:,);
  lastName的=新AutoTextEditField(姓,);
  电子邮件=新EmailAddressEditField(电子邮件::,);  //创建用户输入的选择领域
  的String []项目= {男,女};
  性别=新ObjectChoiceField(性别,项目);
  //创建复选框栏
  状态=新CheckBoxField字段(激活,真正的);
  //创建按钮字段,添加使用监听功能
  保存=新ButtonField字段(保存,ButtonField.CONSUME_CLICK);
  save.setChangeListener(新FieldChangeListener()
  {
   公共无效fieldChanged(场场,诠释上下文)
   {
    保存();
   }
  });
  关闭=新ButtonField字段(关闭,ButtonField.CONSUME_CLICK);
  close.setChangeListener(新FieldChangeListener()
  {
   公共无效fieldChanged(场场,诠释上下文)
   {
    的OnClose();
   }
  });
  //添加输入字段到主屏幕
  mainScreen.add(名字);
  mainScreen.add(lastName的);
  mainScreen.add(电子邮件);
  mainScreen.add(性别);
  mainScreen.add(状态);
  //添加按钮到主屏幕
  mainScreen.add(接近);
  mainScreen.add(保存);  //添加菜单项  mainScreen.addMenuItem(saveItem);
  mainScreen.addMenuItem(的getItem);
  //主推屏
  pushScreen(mainScreen);
 }
//添加功能的菜单项saveItem
私人菜单项saveItem =新菜单项(保存,110,10)
{
  公共无效的run()
  {
   //调用保存方法
   保存();
  }
};
//添加功能的菜单项saveItem
私人菜单项的getItem =新菜单项(GET,110,11)
{
 //正在运行的线程此菜单项
 公共无效的run()
 {
  //线程同步
  同步(存储)
  {
   //获取持久化对象的内容
   _data =(向量)store.getContents();
   //空对象检查
   如果(!_data.isEmpty())
   {
    //如果不为空
    //创建存储信息类的新对象
    StoreInfo信息=(StoreInfo)
    //返回向量的最后一个组件
    _data.lastElement();
    在字符串中检索//存储信息
    的getFirstName =(info.getElement(StoreInfo.NAME));
    getLastName =(info.getElement(StoreInfo.LastNAME));
    getEmail =(info.getElement(StoreInfo.EMail));
    getGender =(info.getElement(StoreInfo.GenDer));
    的getStatus =(info.getElement(StoreInfo.setStatus));    //调用show方法
    显示();
   }
  }
 }
};
//编码持久化存储
静态的 {
店内=
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
同步(存储){
如果(store.getContents()== NULL){
store.setContents(新向量());
store.commit();
}
}
_data =新的向量();
_data =(向量)store.getContents();}
//类新的存储信息实现持久化
私有静态final类StoreInfo实现持久化
{
 //声明变量
 私人矢量_elements;
 公共静态最终诠释NAME = 0;
 公共静态最终诠释姓氏= 1;
 公共静态最终诠释的EMail = 2;
 公共静态最终诠释性别= 3;
 公共静态最终诠释setStatus = 4; 公共StoreInfo()
 {
  _elements =新的向量(5);
  的for(int i = 0; I< _elements.capacity(); ++ I)
  {
   _elements.addElement(新的String());
  }
 } 公共字符串getElement(INT ID)
 {
  返回(字符串)_elements.elementAt(ID);
 }
 公共无效setElement(INT ID,字符串值)
 {
  _elements.setElementAt(值,ID);
 }
}
//为展示方法的详细信息
公共无效显示()
{
 Dialog.alert(名称为+的getFirstName ++ getLastName +\\ nGender是+ getGender +\\ NE-邮件:+ getEmail +\\ n状态是的g​​etStatus +);
}
//创建保存方法
公共无效保存()
{
 //创建内部类StoreInfo的对象
 StoreInfo信息=新StoreInfo();
 //获取在输入字段中输入的测试
 info.setElement(StoreInfo.NAME,firstName.getText());
 info.setElement(StoreInfo.LastNAME,lastName.getText());
 info.setElement(StoreInfo.EMail,email.getText());
 info.setElement(StoreInfo.GenDer,gender.toString());
 如果(status.getChecked())
  info.setElement(StoreInfo.setStatus,活动);
 其他
  info.setElement(StoreInfo.setStatus,在活动);
 //添加对象向量的端
 _data.addElement(信息);
 //同步线程
 同步(存储)
 {  store.setContents(_data);
  store.commit();
 }
 //复位输入字段 Dialog.inform(成功!);
 firstName.setText(NULL);
 lastName.setText(NULL);
 email.setText();
 gender.setSelectedIndex(男);
 status.setChecked(真);
}
//重写的OnClose方法
公共布尔的OnClose()
{
 System.exit(0);
 返回true;
}
}


解决方案

其实这是完全可以节省一切,但检索只有最后一条记录:

  StoreInfo信息=(StoreInfo)_data.lastElement();

试试这显示每一个记录:

 的for(int i = 0; I< _data.size();我++){
StoreInfo信息=(StoreInfo)_data.elementAt(I)
...
显示();
}

I am developing an application in which I want to store many data entries using persistent store. the problem is whenever new entry is made it replaces the existing entry. here is my code please help me.

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.*;
/*An application in which user enters the data. this data is displayed when user press the save button*/
public class Display extends UiApplication {
/*declaring Strings to store the data of the user*/
 String getFirstName;
 String getLastName;
 String getEmail;
 String getGender;
 String getStatus;
 /*declaring text fields for user input*/
 private AutoTextEditField firstName;

 private AutoTextEditField lastName;

 private EmailAddressEditField email;
/*declaring choice field for user input*/
 private ObjectChoiceField gender;
 /*declaring check box field for user input*/
 private CheckboxField status;
 //Declaring button fields
 private ButtonField save;
 private ButtonField close;

 /*declaring vector*/
 private static Vector _data;
 /*declaring persistent object*/
 private static PersistentObject store;

 /*creating an entry point*/
public static void main(String[] args) 
{
 /*creating instance of the class */
 Display app = new Display();
 app.enterEventDispatcher();
}

/*creating default constructor*/
 public Display() 
 {
  /*Creating an object of the main screen class to use its functionalities*/
  MainScreen mainScreen = new MainScreen();

  //setting title of the main screen
  mainScreen.setTitle(new LabelField("Enter Your Data"));

  //creating text fields for user input
  firstName = new AutoTextEditField("First Name: ", "");
  lastName= new AutoTextEditField("Last Name: ", "");
  email= new EmailAddressEditField("Email:: ", "");

  //creating choice field for user input
  String [] items = {"Male","Female"};
  gender= new ObjectChoiceField("Gender",items);
  //creating Check box field
  status = new CheckboxField("Active",true);
  //creating Button fields and adding functionality using listeners
  save = new ButtonField("Save",ButtonField.CONSUME_CLICK);
  save.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    save();
   }
  });
  close = new ButtonField("Close",ButtonField.CONSUME_CLICK);
  close.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    onClose();
   }
  });
  //adding the input fields to the main screen
  mainScreen.add(firstName);
  mainScreen.add(lastName);
  mainScreen.add(email);
  mainScreen.add(gender);
  mainScreen.add(status);
  //adding buttons to the main screen
  mainScreen.add(close);
  mainScreen.add(save);

  //adding menu items

  mainScreen.addMenuItem(saveItem);
  mainScreen.addMenuItem(getItem);
  //pushing the main screen
  pushScreen(mainScreen);
 }
//adding functionality to menu item "saveItem" 
private MenuItem saveItem = new MenuItem("Save", 110, 10) 
{
  public void run() 
  {
   //Calling save method
   save();
  }
};
//adding functionality to menu item "saveItem" 
private MenuItem getItem = new MenuItem("Get", 110, 11) 
{
 //running thread for this menu item
 public void run() 
 {
  //synchronizing thread
  synchronized (store) 
  {
   //getting contents of the persistent object
   _data = (Vector) store.getContents();
   //checking for empty object
   if (!_data.isEmpty()) 
   {
    //if not empty
    //create a new object of Store Info class
    StoreInfo info = (StoreInfo)
    //returning last component of the vector
    _data.lastElement();
    //storing information retrieved in strings
    getFirstName = (info.getElement(StoreInfo.NAME));
    getLastName  = (info.getElement(StoreInfo.LastNAME));
    getEmail   = (info.getElement(StoreInfo.EMail));
    getGender   =  (info.getElement(StoreInfo.GenDer));
    getStatus  = (info.getElement(StoreInfo.setStatus));

    //calling the show method
    show();
   }
  }
 }
};
//coding for persistent store
static {
store =
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
_data = new Vector();
_data = (Vector) store.getContents();

}
//new class store info implementing persistable
private static final class StoreInfo implements Persistable 
{
 //declaring variables
 private Vector _elements;
 public static final int NAME = 0;
 public static final int LastNAME = 1;
 public static final int EMail= 2;
 public static final int GenDer = 3;
 public static final int setStatus = 4;

 public StoreInfo() 
 {
  _elements = new Vector(5);
  for (int i = 0; i < _elements.capacity(); ++i) 
  {
   _elements.addElement(new String(""));
  }
 }

 public String getElement(int id) 
 {
  return (String) _elements.elementAt(id);
 }
 public void setElement(int id, String value) 
 {
  _elements.setElementAt(value, id);
 }
}
//details for show method
public void show()
{
 Dialog.alert("Name is "+getFirstName+" "+getLastName+"\nGender is "+getGender+"\nE-mail: "+getEmail+"\nStatus is "+getStatus);
}
//creating save method
public void save()
{
 //creating an object of inner class StoreInfo
 StoreInfo info = new StoreInfo();
 //getting the test entered in the input fields
 info.setElement(StoreInfo.NAME, firstName.getText());
 info.setElement(StoreInfo.LastNAME,lastName.getText());
 info.setElement(StoreInfo.EMail, email.getText());
 info.setElement(StoreInfo.GenDer,gender.toString());
 if(status.getChecked())
  info.setElement(StoreInfo.setStatus, "Active");
 else
  info.setElement(StoreInfo.setStatus, "In Active");
 //adding the object to the end of the vector
 _data.addElement(info);
 //synchronizing the thread
 synchronized (store) 
 {

  store.setContents(_data);
  store.commit();
 }
 //resetting the input fields

 Dialog.inform("Success!");
 firstName.setText(null);
 lastName.setText(null);
 email.setText("");
 gender.setSelectedIndex("Male");
 status.setChecked(true);
}
//overriding onClose method
public boolean onClose()
{
 System.exit(0);
 return true;
}
}

解决方案

Actually it's saves everything perfectly, but retrieving only the last one record:

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

Try this to display every record:

for (int i = 0; i < _data.size(); i++) {
StoreInfo info = (StoreInfo)_data.elementAt(i);
...
show();
}

这篇关于黑莓持久存储区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆