问题
I have this code which works well:
static {
myStore = PersistentStore.getPersistentObject(MY_LONG_KEY);
myList = (Vector) myStore.getContents();
if (_storedStations == null) {
myList = new Vector(4);
// prepopulate with fake data
myList.addElement(new MyItem("Eins"));
myList.addElement(new MyItem("Zwei"));
myList.addElement(new MyItem("Drei"));
myList.addElement(new MyItem("Vier"));
myStore.setContents(myList);
}
}
class MyItem implements Persistable {
.....
}
probably because the PersistentObject doc says:
Implicit persistance
Notice that some classes are implicitly persistable: Boolean, Byte, Character, Integer, Long, Object, Short, String, Vector, Hashtable.
Also note that, when you persist an object, any persistable object it refers to will also get persisted.
But what about other data structures? For example - instead of the Vector used above - I'd like to have a Hashtable with keys: Strings and values: MyItems. How can I make it Persistable and ensure that MyItem contents is really stored, no matter how complex that object is?
And why is IntHashtable listed as Persistable, but Hashtable is not?
Thank you! Alex
回答1:
You listed Hashtable yourself when you wrote the part about Implicit Persistence, not sure what more you want to verify that it is Persistable. At any rate, if you want MyItems to be Persistable, just do public class MyItems implements Persistable
as its declaration. The way it works is that if you want something to be Persistable, anything that it keeps a copy of (ie, it's member variables) needs to be Persistable as well. So if MyItems needs to store a String, a Vector of Integers, and a MyItem, you need to make sure MyItem is Persistable as well.
This will give you some limitations on what you can save. For instance, you won't be able to create a Persistable MyItems if one of its member variables is a Bitmap. Also note that using a non-Persistable object inside of a method won't prevent you from being able to make the class itself Persistable.
来源:https://stackoverflow.com/questions/6138677/blackberry-how-to-make-a-data-structure-for-example-hashtable-persistable