Add Persistence
One flaw with the To Do MIDlet which was created in the previous tutorial was very basic: the To Do MIDlet always started with a blank slate, with nothing in the To Do list. It would be far better to save the items on the list when the MIDlet was exited, and restore them when the MIDlet is opened. This is called persistence, and is handled in MIDP by RMS (Record Management System).
First the record store object will be created
- Select the startApp node in the Parts list.
- Press the Code Sourcerer button, located beneath the contextual help in the property sheet. Select 'RMS data storage operations...'. Press Next.
- Choose the first option, 'Open a record store...'. Press Next.
- The Code Sourcerer usually creates code "inline", which makes objects available only to the code area which is currently selected. In the case of RMS Record Stores, the Code Sourcerer can also generate code which will be available to the entire MIDlet. Choose this option. Press Next.
- The next page asks for the name of the Record Store. Give the Record Store a descriptive name, ToDoStore. Since the first time the application is run the store will not exist, be sure the checkbox to create the record store if needed is checked. The default value for the Object name, 'rs', is fine. The other option on this page is disabled, since it does not apply in this case. Press Done.
The Code Sourcerer will create the following code in the startApp area
try {
if (rs == null) rs = RecordStore.openRecordStore("ToDoStore",true);
}
catch ( RecordStoreFullException excpt0 ) {
}
catch ( RecordStoreNotFoundException excpt1 ) {
}
catch ( RecordStoreException excpt2 ) {
}
As the comment notes, The Code Sourcerer also added code to the declarations and destroyApp code areas. Since the Code Sourcerer added code to the declarations area, the MIDlet will need to be reloaded before this code will work.
Next we need to create the code which reads the records from the Record store and adds them to the list.
- Press the Code Sourcerer Button. Select 'RMS data storage operations...'. Press Next.
- Choose 'Read a record from a record store...'. Press Next.
- The Record Store object is named rs, so the default value is Ok. press Next.
- We want to read all the records, so choose 'Read all records into a String array...'. Press Next.
- The default values of 'String[]' and 'theRecords' are Ok. Press Done.
The Code Sourcerer will generate code that creates a String Array. Next you want to add this string array to the items list.
- Press the Code Sourcerer Button. Choose 'Change a property of an existing part...'. Press Next.
- Choose 'items'. Press Next.
- The Code Sourcerer now presents you with a list of the items List's properties that can be changed. Choose 'Add an array of Strings to the List...'. Press Next.
- The Code Sourcerer now asks you for the name of the String array. Type in 'theRecords', the array which was just created. We don't want any images in the list, so the default value of no images is fine. Press Done.
This will generate the code which reads the records into the List item at startup time. Next you need to write the code that writes new records when they are added.
- Select the Code after area of the Ok command in the Parts tree.
- Press the Code Sourcerer Button. Choose 'RMS data storage operations...'. Press Next.
- Choose 'Add a record to a record Store...'. Press Next.
- The default name of the Record Store object, 'rs', is Ok. Press Next.
- Choose to enter 'Text string' information. Press Next.
- The text should come from another part, the editor. Select the second option and choose editor from the drop-down menu. Press Done.
You have now finished designing your RMS To Do list. You may test by adding a few items to the To Do list. The next time you open the MIDlet, you will see the items.
Save your work by choosing Exit from the File menu in the Composer. Choose Save All to save your changes.
| Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |