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
bagir
Posts: 52
Registered: ‎06-24-2010
My Carrier: 3

Persistent Store Question

Hi All,

 

I need Help, I'm building Application that save some data in a device without using database application such sqlite, because I use JDE 4.5. I learn from thesample how to save persistent store data and to display it., but the data display is only one line of data.  Please help how to save and display more than only one line of data on the screen?    The code is below:

 public MusicStores() { 
  MainScreen mainScreen = new MainScreen(); 
  mainScreen.setTitle(new LabelField("Favourite Music Store")); 
   namefield = new AutoTextEditField("Store Name: ", ""); 
   mainScreen.add(namefield); 
  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()); 
  

    _data.addElement(info); 
    synchronized (store) { 
     store.setContents(_data); 
     store.commit();
    } 
    Dialog.inform("Success!"); 
     namefield.setText(null); 
   
   } 
  }; 
  
   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));   
  
     } 
    } 
   } 
  }; 
 
   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 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 Vector getElements(int id) { 
        return (Vector) _elements.elementAt(id); 
      } */
    public void setElement(int id, String value) { 
     _elements.setElementAt(value, id); 
   } 
  
    } 

 

Thanks

 

Best Regards

Bagir

 

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

Re: Persistent Store Question

an editfield is not the correct field to display multiple information, most likely.

you could use a listfield (if you want to list the info), or iterate through the vector and use a labelfield (or editfield) for each element.

----------------------------------------------------------
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
bagir
Posts: 52
Registered: ‎06-24-2010
My Carrier: 3

Re: Persistent Store Question

I'm sorry, after creating ListField, how to add the data to the ListField and implement it..Please Help me?

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

Re: Persistent Store Question

you have a listfieldcallback where you add the elements.

you can search the forums and/or knowledge base to find some samples.

----------------------------------------------------------
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
bagir
Posts: 52
Registered: ‎06-24-2010
My Carrier: 3

Re: Persistent Store Question

I have got the sample to put The String data to the list Field with the ListCallBack, the code is like this :

  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(); 
      
      fieldOne = new String("Field one label");
      fieldTwo = new String("Field two label");
      fieldThree = new String("Field three label");
      
      myList.insert(0);
      myList.insert(1);
      myList.insert(2);
      myCallback.insert(fieldOne, 0);
      myCallback.insert(fieldTwo, 1);
      myCallback.insert(fieldThree, 2);
     
     
     } 
    } 

 From the above code I want to change the "Field one Label", "Field two Label" and "Field three Label" with the data from StoreInfo...I try to change the 

 

public void insert(String toInsert, int index) { 

with

public void insert(Vector toInsert, int index) { 

 

 private class ListCallback implements ListFieldCallback {
	   
	   private Vector listElements = new Vector(); 
	   
	   public void drawListRow(ListField list, Graphics g, int index, int y, 
			   int w) { 
			   String text = (String)listElements.elementAt(index); 
			   g.drawText(text, 0, y, 0, w);
			   }
	   public Object get(ListField list, int index) { 
		   return listElements.elementAt(index); 
		   }
	   public int getPreferredWidth(ListField list) { 
		   return Display.getWidth(); 
		   }
	public int indexOfList(ListField listField, String prefix, int start) {
		// TODO Auto-generated method stub
		return 0;
	}
	 public void insert(String toInsert, int index) {        
         listElements.addElement(toInsert);
     }
	  

 

But the ClassCastException is displayed when the application is executed, please help me how to convert this StoreInfo data to be able to be displayed on the list from the code above?

 

 

 

Please use plain text.
Regular Contributor
bagir
Posts: 52
Registered: ‎06-24-2010
My Carrier: 3

Re: Persistent Store Question

I have change the code like below :

 

 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.getElements(StoreInfo.NAME));   
      
      fieldOne = new Vector();
      for(int i=1; i<fieldOne.capacity(); i++)
      {
      fieldOne.setElementAt(info.getElements(StoreInfo.NAME), i);
      myList.insert(i);
      myCallback.insert(fieldOne, i);
      }}
     } 
   } 
  }; 
 
   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 StoreInfo() { 
     _elements = new Vector(4); 
     for (int i = 0; i < _elements.capacity(); ++i) { 
      _elements.addElement(new String("")); 
    } 
   } 
     public String getElements(int id) { 
        return (String) _elements.elementAt(id); 
      } 
    
    public void setElement(int id, String value) { 
     _elements.setElementAt(value, id); 
   } 
  
    } 
   private class ListCallback implements ListFieldCallback {
	   
	   private Vector listElements = new Vector(); 
	   
	   public void drawListRow(ListField list, Graphics g, int index, int y, 
			   int w) { 
			   String text = (String)listElements.elementAt(index); 
			   g.drawText(text, 0, y, 0, w);
			   }
	   public Object get(ListField list, int index) { 
		   return listElements.elementAt(index); 
		   }
	   public int getPreferredWidth(ListField list) { 
		   return Display.getWidth(); 
		   }
	public int indexOfList(ListField listField, String prefix, int start) {
		// TODO Auto-generated method stub
		return 0;
	}
	 public void insert(Vector fieldOne, int index) {        
         listElements.addElement(fieldOne);
     }
	  

 

After I execute the application it displays "ArrayIndexOutOfBoundsException", please help me i'm getting stagnant..what is the mistake in my code above?

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,521
Registered: ‎07-14-2008

Re: Persistent Store Question

Have you actually stepped through this code and checked the values as you go?  Have you determined what line this error is occurring at and which index is too big or too small?

 

I would like to think that you can solve this problem by yourself. 

 

I've no idea where the problem is actually occurring but I have a guess.  As a hint, size() and capacity() have different meanings.  I recommend that you review the API.

 

And next time at least tell us what line the exception is happening on and which index is wrong. 

Please use plain text.
Regular Contributor
bagir
Posts: 52
Registered: ‎06-24-2010
My Carrier: 3

Re: Persistent Store Question

Thank you for answering my question, I have Changed the code to be like this :

 

public class MusicStores extends UiApplication { 

	private AutoTextEditField namefield; 
	private ListField listKu; 
	private Vector _attendees;
	private static Vector _data; 
	private static PersistentObject store; 
	Vector fieldOne;
	String fieldOk;
	// String fieldTwo;
	// String fieldThree;
	ListField myList;
	ListCallback myCallback ;
	public static void main(String[] args) { 
		MusicStores app = new MusicStores(); 
		app.enterEventDispatcher(); 
	} 
	public MusicStores() { 
		MainScreen mainScreen = new MainScreen(); 
		mainScreen.setTitle(new LabelField("Test")); 
		namefield = new AutoTextEditField(" Name: ", "");
		myList = new ListField(0,ListField.MULTI_SELECT);
		myCallback = new ListCallback();
		myList.setCallback(myCallback);
		mainScreen.add(myList);
		mainScreen.add(namefield);
		mainScreen.addMenuItem(saveItem); 
		mainScreen.addMenuItem(getItem); 
		pushScreen(mainScreen); 
	} 
	/*private void loadList(){
		listKu.setSize(_data.size());
	}*/
	private MenuItem saveItem = new MenuItem("Save", 110, 10) { 
		public void run() { 
			StoreInfo info = new StoreInfo(); 
			info.setElement(StoreInfo.NAME, namefield.getText()); 
			_data.addElement(info); 
			synchronized (store) { 
				store.setContents(_data); 
				store.commit();
			} 
			Dialog.inform("Success!"); 
			namefield.setText(null); 

		} 
	}; 
	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.getElements(StoreInfo.NAME));   
	      
	      fieldOne = new Vector();
	      for(int i=0; i<fieldOne.size(); i++)
	      {
	      fieldOne.setElementAt(info.getElements(StoreInfo.NAME), i);
	      myList.insert(i);
	      myCallback.insert(fieldOne, i);
	      }}
	     } 
	   } 
	  }; 
	 
	   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 StoreInfo() { 
	     _elements = new Vector(1); 
	     for (int i = 0; i < _elements.capacity(); ++i) { 
	      _elements.addElement(new String("")); 
	    } 
	   } 
	     public String getElements(int id) { 
	        return (String) _elements.elementAt(id); 
	      } 
	    
	    public void setElement(int id, String value) { 
	     _elements.setElementAt(value, id); 
	   } 
	  
	    } 
	   private class ListCallback implements ListFieldCallback {
		   
		   private Vector listElements = new Vector(); 
		   
		   public void drawListRow(ListField list, Graphics g, int index, int y, 
				   int w) { 
				   String text = (String)listElements.elementAt(index); 
				   g.drawText(text, 0, y, 0, w);
				   }
		   public Object get(ListField list, int index) { 
			   return listElements.elementAt(index); 
			   }
		   public int getPreferredWidth(ListField list) { 
			   return Display.getWidth(); 
			   }
		public int indexOfList(ListField listField, String prefix, int start) {
			// TODO Auto-generated method stub
			return 0;
		}
		 public void insert(Vector fieldOne, int index) {        
	         listElements.addElement(fieldOne);
	     }
	   }
}

 

 

I just change one line of code  that is:

 

 for(int i=0; i<fieldOne.capacity(); i++)
	      {

 to be like this one:

 

 for(int i=0; i<fieldOne.size(); i++)
	      {

 

When I execute, the error is not displayed anymore, but the text that has been stored are still not displayed also, The list is still empty. I have debugged but i'm stag where to find the mistake. Please help me solve where is the mistake in the above code....

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,521
Registered: ‎07-14-2008

Re: Persistent Store Question

And have you put a break point in your drawListRow to see if it is called?

 

How many rows have you told the listfield that it should display?

 

I'm not trying to be mean here, but I really think that you can sort this out yourself. 

 

I admit that ListField is not easy.  There are samples using ListField and everyone has a different way of using it.  But the principles still remain the same.  Your ListField just displays the rows you tell it to, and it displays these using drawListRow..  You need to get your head around that process. 

Please use plain text.
Developer
BB-Dude
Posts: 516
Registered: ‎07-23-2010

Re: Persistent Store Question

If you just need a simple list then use the ObjectListField and use the set function and pass it your Obejct array. All you have to do is overide the toString function of your object. The ObjectListField will display the toString of your objects in a list.

Please use plain text.