如何在 Blackberry 中使用持久对象存储?

发布于 2024-09-24 18:58:06 字数 72 浏览 0 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2024-10-01 18:58:06

在此示例中,我将向量存储在持久存储中。

您必须提供一个商店 ID,该 ID 应该是 long 类型。我通常通过将完全限定的应用程序类名连接到某个字符串来创建它,使其在我的应用程序中独一无二。

//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.

从存储加载 Vector 的示例:

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();

初始化此存储并将其首次保存到磁盘的示例:

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

如果要提交更改(也称为将更改保存到磁盘),则需要重复下面两行。设置内容(OBJ);和提交()。

您可以存储以下内容,而无需执行任何特殊操作:

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 : http: //docs.blackberry.com/en/developers/deliverables/17952/Storing_objects_persistently_1219782_11.jsp

要存储您自己的类,它们(以及所有子类)必须实现“Persistable”接口。我建议您这样做,因为卸载应用程序时这些存储会自动清理。这是因为,当存储中的“任何”引用的类名不再具有与其关联的应用程序时,操作系统会清理存储的对象。因此,如果您的商店仅使用字符串,则它永远不会被清理。

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

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();

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 : http://docs.blackberry.com/en/developers/deliverables/17952/Storing_objects_persistently_1219782_11.jsp

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文