×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Problem with saving and retrieving text with the RMS

    Hi everyone,

    I'm new to the Nokia community and to developing Java ME apps, and am really excited at the possibilities!

    I have recently developed a mobile app, and everything is done, except for persisting the data.

    I'm having trouble with my code. I have a String item named "lblBalance", and I want to have this text persisted - i.e. the the next time my app runs, the same text must display.

    I'm using the RMS as shown in the code below to save the data in the lblBalance item to my record store; it's under the method "saveCurrentState()"

    Code:
    public void saveCurrentState()
        {
    
       RecordStore myStore = null;
    
       try {
    
          myStore = RecordStore.openRecordStore("CashsterDB", true);
    
          int n = myStore.getNumRecords();
    
          // create the byte array representations for storing
    
          byte[] bal = lblBalance.getText().getBytes();
    
    
    
          // do we already have a record set to use?
    
          if(n == 0) {
    
             // no - create new records
    
             myStore.addRecord(bal, 0,
    
                               bal.length);
    
    
          }
    
          else {
    
             // use existing records
    
             myStore.setRecord(1, bal,
    
                               0, bal.length);
    
          }
    
       }
    
       catch(Exception ex) {
    
          // omitted for brevity - but you should do something
    
          // with this
    
       }
    
       finally {
    
          if(myStore != null) {
    
             try {
    
                myStore.closeRecordStore();
    
             }
    
             catch(Exception ex) {
    
                // ignore
    
             }
    
          }
    
       }
    
    
        }
    that above code is for saving to the RMS.

    Below, within the loadCurrentState() method, the app is supposed to load whatever data is in the record store to the string item lblBalance.

    Code:
     public void loadCurrentState()
        {
              RecordStore myStore = null;
    
       try {
    
          myStore = RecordStore.openRecordStore("CashsterDB", true);
    
          int n = myStore.getNumRecords();
    
          if(n == 0) {
    
            this.lblBalance.setText("0");
    
          }
    
          else {
                String in;
                in = new String(myStore.getRecord(1));
                this.lblBalance.setText(in);
          }
    
       }
    
       catch(Exception ex) {
    
          // TODO: do something with exception (ommitted for brevity)
    
       }
    
       finally {
    
          if(myStore != null) {
    
             try {
    
                myStore.closeRecordStore();
    
             }
    
             catch(Exception ex) {
    
                // ignore
    
             }
    
          }
        }
        }
    Those two methods, saveCurrentState() and loadCurrentState() are called at the following points:

    Code:
       public void startApp() {
            if (midletPaused) {
                resumeMIDlet ();
            } else {
                initialize ();
                this.loadCurrentState();
                startMIDlet ();
               
            }
            midletPaused = false;
        }
    Code:
     public void pauseApp() {
            midletPaused = true;
            this.saveCurrentState();
        }
    
       
        public void destroyApp(boolean unconditional) {
            this.saveCurrentState();
        }
    However, I must be doing something wrong, because it's not working. Whenever I start the app again in the emulator, and on my Nokia device (N79) (after setting the lblBalance item with some text via a textBox — this part works well) the lblBalance item returns to its default value ("0" — the value I've pre-set within Netbeans GUI designer).

    Please can someone help urgently, I really need to get this problem fixed! Thanks in advance

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Problem with saving and retrieving text with the RMS

    initialize ();
    startMIDlet ();
    Cut paste the code for these methods, check if you are setting the IbLBalance StringItem with '0' from startMIDlet()?

    Put some Alerts in your loadCurrentState() method and saveCurrentState() methods to see what you are saving what you are reading from RMS on the device.

    thanks,
    ~Amitabh

    thanks,
    ~Amitabh

  3. #3
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: Problem with saving and retrieving text with the RMS

    Hello Radst3r,

    how do you perform RecordStore closing?

    Related article: http://developers.sun.com/mobility/m...s/databaserms/

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

Similar Threads

  1. Byte primitive limit in saving Images to RMS
    By seaumus in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2009-02-05, 11:49
  2. saving and retrieving settings item....
    By impankaj24 in forum Symbian
    Replies: 2
    Last Post: 2006-11-14, 05:21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×