views:

1350

answers:

3

I am developing an application in which I want to store many data entries using persistent store. the problem is whenever new entry is made it replaces the existing entry. here is my code please help me.

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.*;
/*An application in which user enters the data. this data is displayed when user press the save button*/
public class Display extends UiApplication {
/*declaring Strings to store the data of the user*/
 String getFirstName;
 String getLastName;
 String getEmail;
 String getGender;
 String getStatus;
 /*declaring text fields for user input*/
 private AutoTextEditField firstName;

 private AutoTextEditField lastName;

 private EmailAddressEditField email;
/*declaring choice field for user input*/
 private ObjectChoiceField gender;
 /*declaring check box field for user input*/
 private CheckboxField status;
 //Declaring button fields
 private ButtonField save;
 private ButtonField close;

 /*declaring vector*/
 private static Vector _data;
 /*declaring persistent object*/
 private static PersistentObject store;

 /*creating an entry point*/
public static void main(String[] args) 
{
 /*creating instance of the class */
 Display app = new Display();
 app.enterEventDispatcher();
}

/*creating default constructor*/
 public Display() 
 {
  /*Creating an object of the main screen class to use its functionalities*/
  MainScreen mainScreen = new MainScreen();

  //setting title of the main screen
  mainScreen.setTitle(new LabelField("Enter Your Data"));

  //creating text fields for user input
  firstName = new AutoTextEditField("First Name: ", "");
  lastName= new AutoTextEditField("Last Name: ", "");
  email= new EmailAddressEditField("Email:: ", "");

  //creating choice field for user input
  String [] items = {"Male","Female"};
  gender= new ObjectChoiceField("Gender",items);
  //creating Check box field
  status = new CheckboxField("Active",true);
  //creating Button fields and adding functionality using listeners
  save = new ButtonField("Save",ButtonField.CONSUME_CLICK);
  save.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    save();
   }
  });
  close = new ButtonField("Close",ButtonField.CONSUME_CLICK);
  close.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    onClose();
   }
  });
  //adding the input fields to the main screen
  mainScreen.add(firstName);
  mainScreen.add(lastName);
  mainScreen.add(email);
  mainScreen.add(gender);
  mainScreen.add(status);
  //adding buttons to the main screen
  mainScreen.add(close);
  mainScreen.add(save);

  //adding menu items

  mainScreen.addMenuItem(saveItem);
  mainScreen.addMenuItem(getItem);
  //pushing the main screen
  pushScreen(mainScreen);
 }
//adding functionality to menu item "saveItem" 
private MenuItem saveItem = new MenuItem("Save", 110, 10) 
{
  public void run() 
  {
   //Calling save method
   save();
  }
};
//adding functionality to menu item "saveItem" 
private MenuItem getItem = new MenuItem("Get", 110, 11) 
{
 //running thread for this menu item
 public void run() 
 {
  //synchronizing thread
  synchronized (store) 
  {
   //getting contents of the persistent object
   _data = (Vector) store.getContents();
   //checking for empty object
   if (!_data.isEmpty()) 
   {
    //if not empty
    //create a new object of Store Info class
    StoreInfo info = (StoreInfo)
    //returning last component of the vector
    _data.lastElement();
    //storing information retrieved in strings
    getFirstName = (info.getElement(StoreInfo.NAME));
    getLastName  = (info.getElement(StoreInfo.LastNAME));
    getEmail   = (info.getElement(StoreInfo.EMail));
    getGender   =  (info.getElement(StoreInfo.GenDer));
    getStatus  = (info.getElement(StoreInfo.setStatus));

    //calling the show method
    show();
   }
  }
 }
};
//coding for persistent store
static {
store =
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
_data = new Vector();
_data = (Vector) store.getContents();

}
//new class store info implementing persistable
private static final class StoreInfo implements Persistable 
{
 //declaring variables
 private Vector _elements;
 public static final int NAME = 0;
 public static final int LastNAME = 1;
 public static final int EMail= 2;
 public static final int GenDer = 3;
 public static final int setStatus = 4;

 public StoreInfo() 
 {
  _elements = new Vector(5);
  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);
 }
}
//details for show method
public void show()
{
 Dialog.alert("Name is "+getFirstName+" "+getLastName+"\nGender is "+getGender+"\nE-mail: "+getEmail+"\nStatus is "+getStatus);
}
//creating save method
public void save()
{
 //creating an object of inner class StoreInfo
 StoreInfo info = new StoreInfo();
 //getting the test entered in the input fields
 info.setElement(StoreInfo.NAME, firstName.getText());
 info.setElement(StoreInfo.LastNAME,lastName.getText());
 info.setElement(StoreInfo.EMail, email.getText());
 info.setElement(StoreInfo.GenDer,gender.toString());
 if(status.getChecked())
  info.setElement(StoreInfo.setStatus, "Active");
 else
  info.setElement(StoreInfo.setStatus, "In Active");
 //adding the object to the end of the vector
 _data.addElement(info);
 //synchronizing the thread
 synchronized (store) 
 {

  store.setContents(_data);
  store.commit();
 }
 //resetting the input fields

 Dialog.inform("Success!");
 firstName.setText(null);
 lastName.setText(null);
 email.setText("");
 gender.setSelectedIndex("Male");
 status.setChecked(true);
}
//overriding onClose method
public boolean onClose()
{
 System.exit(0);
 return true;
}
}
+4  A: 

Actually it's saves everything perfectly, but retrieving only the last one record:

StoreInfo info = (StoreInfo)_data.lastElement();

Try this to display every record:

for (int i = 0; i < _data.size(); i++) {
StoreInfo info = (StoreInfo)_data.elementAt(i);
...
show();
}
Max Gontar
U are great. its really working thank you. i owe u now.
it's really nothing ) you're welcome!
Max Gontar
A: 

hi there,

I am going thru your example. Can you show how to retrieve all your records?

Thanx!

A: 

It wont work for me where I have to place the for loop....