Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified
Accepted Solution

multiple persistent objects

newbie here.... well hardware engineer turn wanna be java programmer. Working on a demo project for work and need to figure out how to store multiple objects in the device persistent store. Read and tried playing with the restaurant example in the developer guide. its working but it only lets me store data for single object. my little programming background not helping any... still can't figure out how to expand the example into storing multiple restaurant information.

 

can some shead some light/hints on how to expand the example for multiple objects?

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: multiple persistent objects

Without getting too technical, you can only store 1 object using 1 ID.  So one obvious solution to your problem is the have multiple persistent IDs.

 

Another option is to use a 'collection' like a Vector, and store the Objects you want at specific indexes.  So say you have three objects you want to store, A, B and C, then you put A in index 0, B in index 1 and C in index 2, then persist the entire Vector.  If you update B, then you put the new version of B in index 1, and persist the Vector again.

 

Does this help?

Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: multiple persistent objects

Peter,

 

thank you for explaing it in laymen’s terms. any other response i would be totally confused otherwise. I think i understand your recommendations. so its okay to um "layer" i don't think this is the right term... but to use the vector collection class on an object that already incorporates a vector?

Developer
fbwing
Posts: 61
Registered: ‎04-02-2008
My Device: Not Specified

Re: multiple persistent objects

ipcTony,

 

   Here is some sample code for storing multiple records to persistent store.  In this example the persistent store is storing only strings.

 

   This is the code for the persistent store.  Necessary package and imports need to be added.

public final class User implements Persistable { private String[] _elements; // Fields in each record public static final int FIRSTNAME = 0; public static final int LASTNAME = 1; public static final int ADDR1 = 2; public static final int ADDR2 = 3; public static final int CITY = 4; public static final int STATE = 5; public static final int ZIP = 6; public static final int PHONE = 7; public User() { // Initialize _elements one item larger than the constants listed above _elements = new String[8]; for (int i=0; i < _elements.length; ++i) { _elements[i] = new String(""); } } public String getElement(int id) { _elements[id]; } public void setElement(int id, String value) { _elements[id] = value; } }

 

 

   Here is a sample app for adding/retrieving records to/from the persistent store.  Necessary package and imports need to be added.

public class Sample extends UiApplication { private static PersistentObject storeUser; static { // Long value = com.sample.usertest.Userdb // Long value = 0x2ba5f8081f7ef332L storeUser = PersistentStore.getPersistentObject(0x2ba5f8081f7ef332L); synchronized(storeUser) { // In this example, create a new table for the data Vector _data = new Vector(); storeUser.setContents(_data); // New user record User u = new User(); // Add some dummy fields u.setElement(User.FIRSTNAME, "John"); u.setElement(User.LASTNAME, "Doe"); u.setElement(User.ADDR1, "123 Any Street"); u.setElement(User.ADDR2, "PO Box 1234"); u.setElement(User.CITY, "Anywhere"); u.setElement(User.STATE, "OH"); u.setElement(User.ZIP, "12345"); u.setElement(User.PHONE, "(999) 555-1212"); // Add user record to vector/table _data.addElement(w); // Cause the vector/table to be written to persisten store storeCallHistory.commit(); u = new User(); u.setElement(User.FIRSTNAME, "Mary"); u.setElement(User.LASTNAME, "Smith"); u.setElement(User.ADDR1, "999 Park Place"); u.setElement(User.CITY, "Monopoly"); u.setElement(User.STATE, "OH"); u.setElement(User.ZIP, "67890"); u.setElement(User.PHONE, "(999) 555-1212"); _data.addElement(w); storeCallHistory.commit(); } public static void main(String[] args) { Sample theApp = new Sample(); theApp.enterEventDispatcher(); } public Sample() { mainscreen = new MainScreen(); fname = new BasicEditField("First Name: ", "", 20, BasicEditField.FIELD_LEFT); lname = new BasicEditField("Last Name: ", "", 20, BasicEditField.FIELD_LEFT); addr1 = new BasicEditField("Address 1: ", "", 25, BasicEditField.FIELD_LEFT); addr2 = new BasicEditField("Address 2: ", "", 25, BasicEditField.FIELD_LEFT); city = new BasicEditField("City: ", "", 25, BasicEditField.FIELD_LEFT); state = new BasicEditField("State: ", "", 25, BasicEditField.FIELD_LEFT); zip = new BasicEditField("Zip: ", "", 5, BasicEditField.FILTER_NUMERIC | BasicEditField.FIELD_LEFT); phone = new BasicEditField("Phone: ", "", 14, BasicEditField.FILTER_PHONE | BasicEditField.FIELD_LEFT); mainscreen.add(fname); mainscreen.add(lname); mainscreen.add(addr1); mainscreen.add(addr2); mainscreen.add(city); mainscreen.add(state); mainscreen.add(zip); mainscreen.add(phone); pushScreen(mainscreen); MenuItem save = new MenuItem("Save", 40, 40) { public void run() { // Save new user synchronized(storeUser) { Vector _data = (Vector)storeUser.getContents(); if (_data == null) { _data = new Vector(); storeUser.setContents(_data); } User newRec = new User(); // Your validation of the data would go here ... // Set values newRec.setElement(User.FIRSTNAME, fname.getText()); newRec.setElement(User.LASTNAME, lname.getText()); newRec.setElement(User.ADDR1, addr1.getText()); newRec.setElement(User.ADDR2, addr2.getText()); newRec.setElement(User.CITY, city.getText()); newRec.setElement(User.STATE, state.getText()); newRec.setElement(User.ZIP, zip.getText()); newRec.setElement(User.PHONE, phone.getText()); // Add record to vector _data.addElement(newRec); // Write data to persistent store storeUser.commit(); } } }; MenuItem login = new MenuItem("Load", 40, 40) { public void run() { String testname = fname.getText(); // Attempt to retrieve a record from persistent store synchronized(storeUser) { Vector _data = (Vector)storeUser.getContents(); // Test if table has not been intialized if (_data == null) { _data = new Vector(); storeUser.setContents(_data); storeUser.commit(); } if (!_data.isEmpty()) { // For performance, it is not recommended to enumerate the records for (int e=0; e<_data.size(); e++) { User u = (User)_data.elementAt(e); if (u.getElement(User.FIRSTNAME).equals(testname) { lname.setText(u.getElement(User.LASTNAME)); addr1.setText(u.getElement(User.ADDR1)); addr2.setText(u.getElement(User.ADDR2)); city.setText(u.getElement(User.CITY)); state.setText(u.getElement(User.STATE)); zip.setText(u.getElement(User.ZIP)); phone.setText(u.getElement(User.PHONE)); } } } } } }; MenuItem quit = new MenuItem("Quit", 40, 40) { public void run() { // Remove screen UiApplication.getUiApplication().popScreen(Sample.this); } }; mainscreen.addMenuItem(save); mainscreen.addMenuItem(login); mainscreen.addMenuItem(quit); } }

 

 

fbwing

Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: multiple persistent objects

WOW... kudos to you too fbwing.

 

The user's on this support site is outstanding with responses and code sharing. makes me want to get both feet wet on this programming stuff.

 

I think i have a better handle on this object storing problem after peter's explanation and your example code. I can see the logic well maybe not the logic but the flow/steps that i need to take.

 

thanks a bunch everyone for the support.  

Developer
fbwing
Posts: 61
Registered: ‎04-02-2008
My Device: Not Specified

Re: multiple persistent objects

No problem.  I'm also a hardware engineer that's been working on figuring all this out (just a few steps ahead of you, that's all).  The sample code from RIM is pretty helpful, but not until you start to figure out what's going on.  Once you figure it out and look back at the samples, you'll see everything was there in "plain sight", but when starting from scratch it seems like it wasn't there.

 

Searching the various forums is also extremely helpful for finding tidbits of code samples that will help you out.  The folks are RIM have done an outstanding job with this forum responding to questions and providing help.

 

I started out using the RIM IDE and simulator working on a project for 6 months before I even tried to run it on a real device.  Amazingly everything worked the first time I ran it on a real device.  Kind of scared me at first, wondering when I was going to run into something that didn't work exactly the same on a real device.  Many will tell you Eclipse is the better way to code/develop, but so far the RIM IDE has worked just fine for me.

 

fbwing

Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: multiple persistent objects

Got it working… :smileysurprised:

fbwing I took your sample code merged bits an pieces with the sample of the restaurant application and now able to store multiple object records. Now I can take this use this new knowledge “thanks to the peps on this forum” and really start on my demo project.
It’s just a restaurant app showing multiple orders and printing it on Bluetooth printer, noting to write home about but a descent starter project.

Thanks everyone...
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: multiple persistent objects

Have one more question about all of this. Its working but I just feel the need to have a better understanding of what i did so I can build upon it.

 

Correct me as i know I'm sure to be wrong...

 

By using a vector (collection) there is one persistent ID and within this collection there are multiple records call this (objects) stored in it at various index. Using this method the persistent ID will always be the same and the objects within this vector can grow or decreas.

 

I can create any number of vectors (collection) types as long as the persistent IDs are different for the same store?

 

thanks.

Developer
fbwing
Posts: 61
Registered: ‎04-02-2008
My Device: Not Specified

Re: multiple persistent objects

Think of the ID as the "filename".  I left the comments above where the storeUser is defined to show how you create the ID.  If you need to create a second store, in your example say you want another store for employees, you would create a new storeEmployee and convert the text "com.sample.usertest.Employeedb" to Long for the ID.  Next you would create the Employee.java file that defines what you are going to store.

 

fbwing

Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: multiple persistent objects

Aha okay I get it the big picture. so we are actually creating multiple persistent store(s) or files and by having multiple stores i can update the different objects as needed. For some reason when reading through the developer guide i gathered that only (1) store is allowed per application. i must have gotten this part confused with IDs but now i understand. My application needs to store some strings call it default app settings. so now I can place this information in aseparate store.

 

I know this site is for Blackberry programmers and not a JAVA 101 class. I really appreciate the help.

 

thanks for clarifing this fbwing.