如何使用黑莓持久对象存储? [英] How do I use the persistent object store in Blackberry?

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

问题描述

我想创建一个简单的CRUD应用程序来测试出黑莓手机的数据处理能力。

I want to create a simple CRUD application to test out the data handling capabilities of the Blackberry.

我如何创建一个简单的存储功能?

How do I create a simple save function?

推荐答案

在这个例子中,我存储持久性存储的载体。

In this example I'm storing a vector in the persistent store.

您必须拿出一个店铺ID,这应该是long类型。我通常concating完全合格的应用程序的类名称的一些字符串,使得它独特的,在我的应用程序创建此。

You have to come up with a store ID, which should be of type long. I usually create this by concating the fully qualified Application's Class name to some string that makes it unique with in my application.

//class Fields...
//Use the application fully qualified name so that you don't have store collisions. 
static String ApplicaitonID = Application.getApplication().getClass().getName();

static String STORE_NAME    = "myTestStore_V1";
long storeId = StringUtilities.stringHashToLong( ApplicationID + STORE_NAME );
private static PersistentObject myStoredObject; 
private static ContentProtectedVector myObjects;
//End class fields.

从商店加载向量的例子:

Example of loading a Vector from the store:

myStoredObject = PersistentStore.getPersistentObject( storeId ); 
myObjects = (ContentProtectedVector) myStoredObject.getContents();
//Print the number of objects in storeage:
System.out.println( myObjects.size() );

//Insert an element and update the store on "disk"...
myObjects.addElement( "New String" );
myStoredObject.setContents(myObjects);
myStoredObject.commit();

初​​始化这家店,并保存到磁盘的第一次的示例:

Example of initializing this store and saving it to disk for the first time:

myStoredObject = PersistentStore.getPersistentObject( storeId ); 
myObjects = (ContentProtectedVector) myStoredObject.getContents();
if(myObjects == null)
    myObjects = new ContentProtectedVector(); 
myStoredObject.setContents(myObjects);
myStoredObject.commit();

如果你想提交更改(即更改保存到磁盘),你需要重复底部两行。 setContents(OBJ);并提交()。

If you want to commit changes (aka save changes to disk), you need to repeat the bottom two lines. setContents(OBJ); and Commit().

您可以存储以下内容,而无需做什么特别的:

You can store the following without having to do anything special:


java.lang.Boolean 
java.lang.Byte 
java.lang.Character 
java.lang.Integer 
java.lang.Long 
java.lang.Object 
java.lang.Short 
java.lang.String 
java.util.Vector 
java.util.Hashtable 

@see:<一href=\"http://docs.blackberry.com/en/developers/deliverables/17952/Storing_objects_persistently_1219782_11.jsp\" rel=\"nofollow\">http://docs.blackberry.com/en/developers/deliverables/17952/Storing_objects_persistently_1219782_11.jsp

要保存自己的类,他们(和所有子类)必须实现持久化的界面。我建议你​​这样做,因为当应用程序被卸载这些商店都会自动清理。这是因为,在OS清除存储的对象时,当任意在商店中引用类名不再具有与之相关联的应用。所以,如果你的小店只使用字符串,它永远不会得到清理。

To store your own Classes, they (and all sub classes) have to implement the "Persistable" interface. I recommend that you do this, as these stores get cleaned up automatically when your application is uninstalled. This is because the OS cleans stored objects up, when "any" referenced classname in the store no longer has an application associated with it. So if your store is only using Strings, it's never going to get cleaned up.

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

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