views:

40

answers:

2

I am developing a blackberry app in jdp plugin for eclipse.I want to store some values froma na array in the flash memory of blackberry device,& also check whether dat value already exits in the memory or not.I am giving the code which i tried to do with persistent object,bt somehw i am nt able to get want i want,plz modify the code where reqd

package com.firstBooks.series7.db;

import java.util.Random;
import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.parser.XMLParser;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;

public class DBMain {
  public static String answer = "";
  public static String selectedAnswer = "";
  public static Question curQuestion;

  public static int currQuesNumber = 1;
  public static int correctAnswerCount = 0;
  public static int totalNumofQuestions = 50 ;

  static int quesNum[] = new int[20];
  static int quesNumNew[];
  static int quesCount = -1;

  static int randomPosition;
  static PersistentObject store;
  static {
    store = PersistentStore.getPersistentObject( 0xf9f8c7a20bc35c51L);
  }

  static{   
    initialize();   
  }

  private static void initialize(){

    Random rgen = new Random();  // Random number generator

    //--- Initialize the array 
    for (int i=0; i<quesNum.length; i++) {
      quesNum[i] = i;
    }

    //--- Shuffle by exchanging each element randomly
    for (int i=0; i< quesNum.length; i++) {
      randomPosition = rgen.nextInt(quesNum.length);

      int temp = quesNum[i];

      quesNum[i] = quesNum[randomPosition];

      quesNum[randomPosition] = temp;

      synchronized(store) {
        if(quesNum[randomPosition]!=quesNum[i]){
          System.out.println("...........i can do it............ ");
          store.setContents(quesNum);
          store.commit();
        }
      }
    }
  }

  /*Changed the code to get a unique random number
   * @author: Venu   
   */
  public static int getQuestionNumber() {
    quesCount++;
    if(quesCount < quesNum.length){
      synchronized(store) {
        int [] quesNumNew = (int[])store.getContents();

        return quesNumNew[quesCount];
      }
    }
    else{ 
      initialize();
      quesCount = -1;
      return getQuestionNumber();
    }
  }
}
A: 

What is the problem you are encountering? Did you try to wrap the array in an object that implements Persistable interface? It is like the Serializable interface in j2se.

also see: http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/util/Persistable.html

"A class must explicitly implement this interface for the system to persistently store instances of the class."

Viele
Arunabha Dutta Choudhury
I think it is really better to try to read and understand the document, then code it up yourself. It is not only a good practice, but also the best practice. Here is a very detailed document on persistence and blacberry platform.http://na.blackberry.com/developers/resources/A13_Storing_Persistent_Data_V2.pdf
Viele
A: 

i want to store 10 values at a time in the memory,& den while storing next 10 values,the pice of code should check whether the values we are going to add to flash memory is already present ot not in the memeory,if not then dose 10 values would be added one by one.Can u plz alter the code whihc i hav given to implement dis

Arunabha Dutta Choudhury