View Single Post
Old 08-08-2005, 11:27 AM   #1
timinator
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Default Data Save & Read Example

Please Login to Remove!

Just for the folks that are getting started with BB development, here is some sample code of a small app that demonstrates how to save and read data using the BB api for Persistent storage, along with using screen fields and buttons. I'm sure that some of the other developers here can offer some further tips on making this snipet better - but at least it's a good start for new BB developers. Enjoy:
Code:
/*
 * DataSave.java
 * © Timothy Trimble, 2005
 * www.timothytrimble.info 
 * Demonstrates how to save and retrieve data
 */

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.util.*;
import java.util.*;

// DataSave.java Class --------------------------------------------------------------------------
public class DataSave extends net.rim.device.api.ui.UiApplication implements FieldChangeListener
{
    private AutoTextEditField inputField;                   //define the edit fields
    private AutoTextEditField anotherField;
    private LabelField appTitle;                            //application title label
    private ButtonField saveButton;                         //define the buttons
    private ButtonField getButton;
    
    //string used for PersistentObject is: com.rim.samples.docs.datasave
    //per the RIM docs, you have to create a unique id for the persistent object
    //this is done by entering a string in IDE, select the string, right click on the string,
    //then select 'Convert to long
    
    //declare the PersistentObject handle
    private static PersistentObject mystore;
    static 
    {
        mystore = PersistentStore.getPersistentObject( 0xe63e08aacd575338L );
    }
    
    public static void main(String[] args)
    {
        DataSave TIinstance = new DataSave();               //create an instance of this class app
        TIinstance.enterEventDispatcher();                  //define a handle for the event processor
    }
    
    public DataSave()
    {
        MainScreen inputScreen = new MainScreen();          //create screen instance
        appTitle = new LabelField("Input Test2");           //define app title
        inputField = new AutoTextEditField("Input1: ","");  //define edit fields
        anotherField = new AutoTextEditField("Input2: ","");
        saveButton = new ButtonField("Save Data");          //define button titles
        getButton = new ButtonField("Get Data");
        saveButton.setChangeListener(this);                 //define the button handles for the event processor
        getButton.setChangeListener(this);
        inputScreen.setTitle(appTitle);                     //post the screen title
        inputScreen.add(inputField);                        //post the input fields on screen
        inputScreen.add(anotherField);
        inputScreen.add(saveButton);                        //post the buttons on the screen
        inputScreen.add(getButton);
        pushScreen(inputScreen);                            //post the screen itself
    }

    public void fieldChanged(Field field, int context) //respond to button events
    {
        if (field == saveButton) //if save data button selected
        {
            String myField1 = inputField.getText(); //read text from first field
            String myField2 = anotherField.getText(); //read text from second field
            String[] fieldInfo = {myField1, myField2}; //store text into string record
            synchronized(mystore) //lock down this thread
            {
                mystore.setContents(fieldInfo); //store the string record
                mystore.commit(); //commit the changes
            }
            Dialog.alert(inputField.getText() + " saved."); //show text from field
        }
        if (field == getButton)  //if get data button selected
        {
            synchronized(mystore) //lock down this thread
            {
                String[] currentInfo = (String[])mystore.getContents(); //read the data
                if (currentInfo == null) //if no data found
                {
                    Dialog.alert("Data not found.");
                } 
                else //if data is found
                {
                    inputField.setText(currentInfo[0]); //get 1st string, put in screen field
                    anotherField.setText(currentInfo[1]); //get 2nd string, put in screen field
                }
            }
        }
    }

}
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline