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
Regular Contributor
walkline
Posts: 81
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom
Accepted Solution

Problem of Persistent Store, need your help!~

[ Edited ]

Forgive and let me make a long story to short.:Devil2:

 

Last few days, I'm according to the document A13_Storing_Persistent_Data_V2 and fantasize to make a useful class for my app, but I'm failed completely, below is the source code wrote in the document.

 

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.util.*; 
import java.util.*; 
 
public class MusicStores extends UiApplication { 
 
  private AutoTextEditField namefield; 
  private AutoTextEditField addressfield; 
  private EditField phonefield; 
  private EditField specialtyfield; 
  private static Vector _data; 
  private static PersistentObject store; 
 
  public static void main(String[] args) { 
    MusicStores app = new MusicStores(); 
    app.enterEventDispatcher(); 
  } 
 
  public MusicStores() { 
    MainScreen mainScreen = new MainScreen(); 
    mainScreen.setTitle(new LabelField("Favourite Music Store")); 
    namefield = new AutoTextEditField("Store Name: ", ""); 
    addressfield = new AutoTextEditField("Address: ", ""); 
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE); 
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT); 
    mainScreen.add(namefield); 
    mainScreen.add(addressfield); 
    mainScreen.add(phonefield); 
    mainScreen.add(specialtyfield); 
    mainScreen.addMenuItem(saveItem); 
    mainScreen.addMenuItem(getItem); 
    pushScreen(mainScreen); 
  } 
 
  private MenuItem saveItem = new MenuItem("Save", 110, 10) { 
    public void run() { 
      StoreInfo info = new StoreInfo(); 
      info.setElement(StoreInfo.NAME, namefield.getText()); 
      info.setElement(StoreInfo.ADDRESS, ddressfield.getText()); 
      info.setElement(StoreInfo.PHONE, phonefield.getText()); 
      info.setElement(StoreInfo.SPECIALTY, specialtyfield.getText()); 
      _data.addElement(info); 
      synchronized (store) { 
        store.setContents(_data); 
        store.commit(); 
      } 
      Dialog.inform("Success!"); 
      namefield.setText(null); 
      addressfield.setText(null); 
      phonefield.setText(""); 
      specialtyfield.setText(""); 
    } 
  }; 
 
  private MenuItem getItem = new MenuItem("Get", 110, 11) { 
    public void run() { 
      synchronized (store) { 
        _data = (Vector) store.getContents(); 
        if (!_data.isEmpty()) { 
          StoreInfo info = (StoreInfo) _data.lastElement(); 
       
          namefield.setText(info.getElement(StoreInfo.NAME));     
          addressfield.setText(info.getElement(StoreInfo.ADDRESS));   
          phonefield.setText(info.getElement(StoreInfo.PHONE));    
          specialtyfield.setText(info.getElement(StoreInfo.SPECIALTY)); 
        } 
      } 
    } 
  }; 
 
  static { 
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
    synchronized (store) { 
      if (store.getContents() == null) { 
        store.setContents(new Vector()); 
        store.commit(); 
      } 
    } 
    _data = new Vector(); 
    _data = (Vector) store.getContents(); 
  } 
 
  private static final class StoreInfo implements Persistable { 
    private Vector _elements; 
    public static final int NAME = 0; 
    public static final int ADDRESS = 1; 
    public static final int PHONE = 2; 
    public static final int SPECIALTY = 3; 
 
    public StoreInfo() { 
      _elements = new Vector(4); 
      for (int i = 0; i < _elements.capacity(); ++i) { 
        _elements.addElement(new String("")); 
      } 
    } 
 
    public String getElement(int id) { 
      return (String) _elements.elementAt(id); 
    } 
 
    public void setElement(int id, String value) { 
      _elements.setElementAt(value, id); 
    } 
  } 
}

 

I put this code in two parts, part one for save info and part two for get info, and they are using the same StoreInfo Class, below is the code of part one.

 

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.util.*; 
import java.util.*; 
 
public class SaveMusicInfo extends UiApplication { 
 
  private AutoTextEditField namefield; 
  private AutoTextEditField addressfield; 
  private EditField phonefield; 
  private EditField specialtyfield; 
  private static Vector _data; 
  private static PersistentObject store; 
 
  public static void main(String[] args) { 
    SaveMusicInfo app = new SaveMusicInfo(); 
    app.enterEventDispatcher(); 
  } 
 
  public SaveMusicInfo() { 
    MainScreen mainScreen = new MainScreen(); 
    mainScreen.setTitle(new LabelField("Favourite Music Store")); 
    namefield = new AutoTextEditField("Store Name: ", ""); 
    addressfield = new AutoTextEditField("Address: ", ""); 
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE); 
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT); 
    mainScreen.add(namefield); 
    mainScreen.add(addressfield); 
    mainScreen.add(phonefield); 
    mainScreen.add(specialtyfield); 
    mainScreen.addMenuItem(saveItem); 
    mainScreen.addMenuItem(getItem); 
    pushScreen(mainScreen); 
  } 
 
  private MenuItem saveItem = new MenuItem("Save", 110, 10) { 
    public void run() { 
      StoreInfo info = new StoreInfo(); 
      info.setElement(StoreInfo.NAME, namefield.getText()); 
      info.setElement(StoreInfo.ADDRESS, ddressfield.getText()); 
      info.setElement(StoreInfo.PHONE, phonefield.getText()); 
      info.setElement(StoreInfo.SPECIALTY, specialtyfield.getText()); 
      _data.addElement(info); 
      synchronized (store) { 
        store.setContents(_data); 
        store.commit(); 
      } 
      Dialog.inform("Success!"); 
      namefield.setText(null); 
      addressfield.setText(null); 
      phonefield.setText(""); 
      specialtyfield.setText(""); 
    } 
  }; 
 
  static { 
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
    synchronized (store) { 
      if (store.getContents() == null) { 
        store.setContents(new Vector()); 
        store.commit(); 
      } 
    } 
    _data = new Vector(); 
    _data = (Vector) store.getContents(); 
  } 
 
  private static final class StoreInfo implements Persistable { 
    private Vector _elements; 
    public static final int NAME = 0; 
    public static final int ADDRESS = 1; 
    public static final int PHONE = 2; 
    public static final int SPECIALTY = 3; 
 
    public StoreInfo() { 
      _elements = new Vector(4); 
      for (int i = 0; i < _elements.capacity(); ++i) { 
        _elements.addElement(new String("")); 
      } 
    } 
 
    public String getElement(int id) { 
      return (String) _elements.elementAt(id); 
    } 
 
    public void setElement(int id, String value) { 
      _elements.setElementAt(value, id); 
    } 
  } 
}

 

Next is the code of part two.

 

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.util.*; 
import java.util.*; 
 
public class GetMusicInfo extends UiApplication { 
 
  private AutoTextEditField namefield; 
  private AutoTextEditField addressfield; 
  private EditField phonefield; 
  private EditField specialtyfield; 
  private static Vector _data; 
  private static PersistentObject store; 
 
  public static void main(String[] args) { 
    GetMusicInfo app = new GetMusicinfo(); 
    app.enterEventDispatcher(); 
  } 
 
  public GetMusicInfo() { 
    MainScreen mainScreen = new MainScreen(); 
    mainScreen.setTitle(new LabelField("Favourite Music Store")); 
    namefield = new AutoTextEditField("Store Name: ", ""); 
    addressfield = new AutoTextEditField("Address: ", ""); 
    phonefield = new EditField("Phone Number: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_PHONE); 
    specialtyfield = new EditField("Music Type: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_DEFAULT); 
    mainScreen.add(namefield); 
    mainScreen.add(addressfield); 
    mainScreen.add(phonefield); 
    mainScreen.add(specialtyfield); 
    mainScreen.addMenuItem(saveItem); 
    mainScreen.addMenuItem(getItem); 
    pushScreen(mainScreen); 
  } 
 
  private MenuItem getItem = new MenuItem("Get", 110, 11) { 
    public void run() { 
      synchronized (store) { 
        _data = (Vector) store.getContents(); 
        if (!_data.isEmpty()) { 
          StoreInfo info = (StoreInfo) _data.lastElement(); 
       
          namefield.setText(info.getElement(StoreInfo.NAME));     
          addressfield.setText(info.getElement(StoreInfo.ADDRESS));   
          phonefield.setText(info.getElement(StoreInfo.PHONE));    
          specialtyfield.setText(info.getElement(StoreInfo.SPECIALTY)); 
        } 
      } 
    } 
  }; 
 
  static { 
    store = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
    synchronized (store) { 
      if (store.getContents() == null) { 
        store.setContents(new Vector()); 
        store.commit(); 
      } 
    } 
    _data = new Vector(); 
    _data = (Vector) store.getContents(); 
  } 
 
  private static final class StoreInfo implements Persistable { 
    private Vector _elements; 
    public static final int NAME = 0; 
    public static final int ADDRESS = 1; 
    public static final int PHONE = 2; 
    public static final int SPECIALTY = 3; 
 
    public StoreInfo() { 
      _elements = new Vector(4); 
      for (int i = 0; i < _elements.capacity(); ++i) { 
        _elements.addElement(new String("")); 
      } 
    } 
 
    public String getElement(int id) { 
      return (String) _elements.elementAt(id); 
    } 
 
    public void setElement(int id, String value) { 
      _elements.setElementAt(value, id); 
    } 
  } 
}

 

I'm runing the Project One (part one), and save some information to store, and then I WANT TO runing the Project Two (part two), but I received an error message that told me there are two same defined class in system, I try to modify the class name of StoreInfo in project two, the last error messge disappeared, instead of a CastClassException error, I'm so sad....

 

For a long time debugging I found some interesting things, look at the below codes in Project Two, please pay attention to the Class name of StoreInfor, not StoreInfo.

 

 private MenuItem getItem = new MenuItem("Get", 110, 11) { 
    public void run() { 
      synchronized (store) { 
        _data = (Vector) store.getContents(); 
        if (!_data.isEmpty()) { 
          StoreInfor info = (StoreInfor) _data.lastElement(); 
        } 
      } 
    } 
  }; 

 

Now the Class name of _data is StoreInfo@xxxxxx (by using _data.getClass().getName()), and Class StoreInfo in Project Two was renamed to StoreInfor, of course, I received a CastClassException error.

 

I finished the story, and I need some guys help to solve this problem, tks a lot!~

Please use plain text.
Developer
simon_hain
Posts: 15,876
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Problem of Persistent Store, need your help!~

you cannot share classes that implement persistable between projects, every class can exist only once.

use a standard persistable class like hashtable if you want to share objects.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
sickned
Posts: 88
Registered: ‎09-25-2008
My Device: Not Specified

Re: Problem of Persistent Store, need your help!~

Or create a lib project containing the persistable class that will use both projects.

Please use plain text.
Regular Contributor
walkline
Posts: 81
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Problem of Persistent Store, need your help!~

Hi, simon, thanks for your explanation, I just want to share some settings value between two apps (background thread & UI), background thread read/save settings and UI read settings only.

 

I don't know how to share objects via hashtable between two apps, and i believe this will made my apps complicated.

 

Maybe I have to change my mind for this issue...

Please use plain text.
Regular Contributor
walkline
Posts: 81
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Problem of Persistent Store, need your help!~

Hi sickned, thanks for your suggest, but I don't want to make things complicated.

Please use plain text.
Developer
sickned
Posts: 88
Registered: ‎09-25-2008
My Device: Not Specified

Re: Problem of Persistent Store, need your help!~

Its not complicated. Its the only and normal way to do what you want.

Please use plain text.
Regular Contributor
walkline
Posts: 81
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Problem of Persistent Store, need your help!~

In fact, I'm also using a SQLite Database to share recorders between my apps, so I made another table to store settings information for myself, of course, I paid more codes to support this.

 

Additional, can you please take me some references that about how to using library for an app, I'm interesting to know it, thanks a lot!

Please use plain text.
Developer
sickned
Posts: 88
Registered: ‎09-25-2008
My Device: Not Specified

Re: Problem of Persistent Store, need your help!~

Creating a blackberry project and inside app descriptor, choose library. Then, every project has to reference this library.
Please use plain text.
Developer
simon_hain
Posts: 15,876
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Problem of Persistent Store, need your help!~

storing values in a hashtable is not really complicated.
http://www.blackberry.com/developers/docs/7.1.0api/java/util/Hashtable.html
you use a key and store/retrieve a value. you can put the whole hashtable into persistant store.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Regular Contributor
walkline
Posts: 81
Registered: ‎01-10-2013
My Device: Bold 9900
My Carrier: China Unicom

Re: Problem of Persistent Store, need your help!~

Yes, I got it, I can reference library, and then?

How to implements the library?
Please use plain text.