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
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified
Accepted Solution

data store issue in mobile memory

thanks to help me

 

Now i work on history issue.

 

In history i have to show last 10 criteria which user enter .

i think ,i have to store that data in phone memory.

 

so please give me any idea how it's possible.

 

i want to support it in bb jde 4.3 and later.

 

please help me as soon as possible.

 

thanks in advance.

 

Please use plain text.
Developer
girishthakr
Posts: 232
Registered: ‎12-15-2008
My Device: 9700
My Carrier: Vodafone India

Re: data store issue in mobile memory

Use Persistent Store to do this...

Please use plain text.
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: data store issue in mobile memory

Using persistent store ,i can store value only single value which is stored previously.

 

i want to store last 10 value. how it's possible?

 

code:

      /*-----------------main1.java----------------------------------*/

         ///button click event code

                  String[] fieldInfo = {loc,mlsno,be,bat,radis,minp,maxp,name2};
                    synchronized(mystore)
                    {
                        mystore.setContents(fieldInfo);
                        mystore.commit();
                    }
                    UiApplication.getUiApplication().pushScreen(new history());

 

 

            /*--------------------------history.java-----------------------------*/

 

            history()
           {
            synchronized(main1.mystore)
            {
                String[] currentInfo = (String[])main1.mystore.getContents();
                if (currentInfo == null)
                {
                    Dialog.alert("Data not found.");
                }
                else
                {
                    h1.add(new LabelField("Location:"+currentInfo[0]));
                    vfm.add(h1);
                    h2.add(new LabelField("Mls No. :"+currentInfo[1]));
                    vfm.add(h2);
                    h3.add(new LabelField("Bed:"+currentInfo[2]));
                    h3.add(new LabelField("     "));
                    h3.add(new LabelField("Bath:"+currentInfo[3]));
                    h3.add(new LabelField("     "));
                    h3.add(new LabelField("radius"+currentInfo[4]));
                    vfm.add(h3);
                    h6.add(new LabelField("Minimum Price:"+currentInfo[5]));
                    vfm.add(h6);
                    h7.add(new LabelField("Maximum Price:"+currentInfo[6]));
                    vfm.add(h7);
                    h8.add(new LabelField("Property type:"+currentInfo[7]));
                    vfm.add(h8);
                    add(vfm);
                }
              }
            
        }
   

 

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: data store issue in mobile memory

[ Edited ]

To me the answer the seems obvious so perhaps I am missing something? 

 

Why not store a collection (like a 10 element Vector) rather than a single array?

Please use plain text.
Developer
girishthakr
Posts: 232
Registered: ‎12-15-2008
My Device: 9700
My Carrier: Vodafone India

Re: data store issue in mobile memory

I agree wit Peter. Store Vector Persistently

 

Pass Vector in place of String array [ ].

Please use plain text.
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: data store issue in mobile memory

hello

 

i use vectore i can store data in vector but i face problem to display that data.

my code and output as below:

 

code:


class tryit extends MainScreen
{
    BasicEditField inputField;  
    BasicEditField anotherField;                         
    ButtonField saveButton;                        
    ButtonField getButton;
    static PersistentObject   mystore = PersistentStore.getPersistentObject( 0xe63e08aacd575338L );
    Vector data;
    tryit()
    {   
        inputField=new BasicEditField("Name:","");
        anotherField=new BasicEditField("SurName:","");
        add(inputField);
        add(anotherField);
        saveButton = new ButtonField("Save Data");          
        getButton = new ButtonField("Get Data");
        
        saveButton.setChangeListener(new FieldChangeListener()
        {
               public void fieldChanged(Field field,int content)
               {    
                    
                    String myField1 = inputField.getText();
                    String myField2 = anotherField.getText();
                    String[] fieldInfo = {myField1, myField2};
                    synchronized(mystore)
                    {    
                       data=(Vector)mystore.getContents();
                       data.addElement(fieldInfo);
                       mystore.setContents(data);
                       mystore.commit();      
                    }
                    Dialog.alert(inputField.getText() + " saved.");
              }
         });
         add(saveButton);  
         
         getButton.setChangeListener(new FieldChangeListener()
         {
               public void fieldChanged(Field field,int content)
               {   
                    synchronized(mystore) //lock down this thread
                    {   
                            data=(Vector)mystore.getContents();
                            System.out.println("size="+data.size());
                            mystore.setContents(data);
                            mystore.commit();   
                            String name=(data.elementAt(0)).toString();        
                            for(int i=0;i<data.size();i++)
                            {
                                System.out.println("element at "+i+" is "+(data.elementAt(i)));
                            }
                              
                     }
                }    
          });
          add(getButton);        
    }
}

 

output:

 

size=6
element at 0 is [Ljava.lang.String;@e605eb6f
element at 1 is [Ljava.lang.String;@bd4c909b
element at 2 is [Ljava.lang.String;@bbd59d07
element at 3 is [Ljava.lang.String;@d471f148
element at 4 is [Ljava.lang.String;@cea38325
element at 5 is [Ljava.lang.String;@61b6dee3

 

u can show that it show me some different value .

 

please help me.

 

Please use plain text.
Developer
girishthakr
Posts: 232
Registered: ‎12-15-2008
My Device: 9700
My Carrier: Vodafone India

Re: data store issue in mobile memory

Hey U missing something here...

 

for(int i=0;i<data.size();i++)
                            {
                                System.out.println("element at "+i+" is "+(data.elementAt(i)));
                            }

 

 

 

Do it like

 

for(int i=0;i<data.size();i++)
                            {
                                System.out.println("element at "+i+" is "+(data.elementAt(i)).toString());
                            }

 

 

And if u notice .... then in ur code....   String name=(data.elementAt(0)).toString();  

 

System.out.println("Name is...  "+name);     This will gives u correct value...

 

 

Press Kudos if resolved.... :smileyhappy:

Please use plain text.
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: data store issue in mobile memory

my above code is store only single value.

so i update my code as below ,but it show me NonPersistentObjectExcption.

please help to me solve it.

 

code:

 

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.*;

class adddata extends MainScreen
{
    BasicEditField inputField;  
    BasicEditField anotherField;                         
    ButtonField saveButton;                        
    ButtonField getButton;
    Vector data;
    static PersistentObject  store = PersistentStore.getPersistentObject(0xe69c43e620187619L);
   
    adddata()
    {   
        
            
        inputField=new BasicEditField("Name:","");
        anotherField=new BasicEditField("SurName:","");
        add(inputField);
        add(anotherField);
        saveButton = new ButtonField("Save Data");          
        
        saveButton.setChangeListener(new FieldChangeListener()
        {
               public void fieldChanged(Field field,int content)
               {     
                    String myField1 = inputField.getText();
                    String myField2 = anotherField.getText();
                
                    StoreInitial info=new StoreInitial();
                    info.setElement(StoreInitial.AMOUNT,myField1);
                    info.setElement(StoreInitial.SOURCE,myField2);
                   
                    synchronized(store)
                    {  
                       data=(Vector)store.getContents();
                       store.setContents(data);
                       data.addElement(info);
                       store.commit();  
                       Dialog.alert(" saved.");     
                    }   
              }
         });
         add(saveButton);        
    }
    
public class StoreInitial
{
      private Vector element;
      public static final int AMOUNT = 0;
      public static final int SOURCE = 1;
     
      public StoreInitial()
      {
            element = new Vector(10);
            for (int i=0; i <element.capacity(); ++i)
            {
                   element.addElement(new String(""));
            }
       }
              
        public void setElement(int id, String value)
        {
                 element.setElementAt(value, id);
        }
              
        public String getElement(int id)
        {
                return (String) element.elementAt(id);
        }
    }
}

 

Please use plain text.
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011
My Device: Not Specified

Re: data store issue in mobile memory

hey i find where is the problem in my code after debugging.

here  store.commit ()   show me exception if i remove it works properly,

but i require  to commit  persistent object because if i do not  commit it  then  simulator lost all data when i close the simulator.    since plese help me to solve  it .

 

code:

 

class adddata extends MainScreen
{
    BasicEditField inputField;  
    BasicEditField anotherField;                         
    ButtonField saveButton;                        
    ButtonField getButton;
    Vector data;
    static PersistentObject  store = PersistentStore.getPersistentObject(0xe69c43e62018

7619L);
   
    adddata()
    {   
        
            
        inputField=new BasicEditField("Name:","");
        anotherField=new BasicEditField("SurName:","");
        add(inputField);
        add(anotherField);
        saveButton = new ButtonField("Save Data");          
        
        saveButton.setChangeListener(new FieldChangeListener()
        {
               public void fieldChanged(Field field,int content)
               {     
                    String myField1 = inputField.getText();
                    String myField2 = anotherField.getText();
                
                    StoreInitial info=new StoreInitial();
                    info.setElement(StoreInitial.AMOUNT,myField1);
                    info.setElement(StoreInitial.SOURCE,myField2);
                   
                    synchronized(store)
                    {  
                       data=(Vector)store.getContents();
                       store.setContents(data);
                       data.addElement(info);
                       store.commit();                        //  <-----------  it show me error                            
                       Dialog.alert(" saved.");     
                    }   
              }
         });
         add(saveButton);        
    }
    
public class StoreInitial
{
      private Vector element;
      public static final int AMOUNT = 0;
      public static final int SOURCE = 1;
     
      public StoreInitial()
      {
            element = new Vector(10);
            for (int i=0; i <element.capacity(); ++i)
            {
                   element.addElement(new String(""));
            }
       }
              
        public void setElement(int id, String value)
        {
                 element.setElementAt(value, id);
        }
              
        public String getElement(int id)
        {
                return (String) element.elementAt(id);
        }
    }
}

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: data store issue in mobile memory

1) Review this to fill in some background knowledge about persistentstore that seems to be missing:

http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

 

2) Since you seem to be going through similar problems as are solved in this Thread, I recommend you review this Thread:

http://supportforums.blackberry.com/t5/Java-Development/Persistent-Store/m-p/1059997#M155866

 

3) When you report problems can you please report what actually happens in full.  In this case I'm assuming you get an Exception thrown - what is it and what is its detail message (<Exception>/toString() will give you this). 

 

Please do all three of the above, don't just find out what the exception is and come back. 

Please use plain text.