views:

109

answers:

5

I am trying to store one value at a time from an array into a vector,so its like that the vector should increase its size dynamically.In my app,when we click start button,the value from the array goes to another page,where it is fetched,& then displayed,but wen i am trying to add that value into a vector & save it in persistent memory of blackberry,i can see only the last has been added into the vector,but i want the vector to one by one add all the values which i sent to the other page,here is the code,can anyone help me out

if ( quesNum != null && quesCount < quesNum.length )
{
   Vector data = new Vector();

   data.addElement ( TestScreen.quesNumber + "" );

   synchronized ( curQues )
   {

      curQuesnew = quesNum[quesCount];
      curQues.setContents ( curQuesnew + "" );
      curQues.commit();
   }

   synchronized ( disques )
   {

      System.out.println ( "value is " + data );
      disques.setContents ( data );
      disques.commit();
   }
   synchronized ( disques )
   {
      data = ( Vector ) disques.getContents(); 
   }

   if ( data.contains ( quesNum[quesCount] + "" ) )
   {
      xyz = quesNum[quesCount++];
      System.out.println ( "i shall overcome........." );

   }
   else
   {
      System.out.println ( "hardwork conqures all........." );
      xyz = quesNum[quesCount];
   }
   return xyz;

this is the value which i am sending to the other page everytime we click next button,so i am trying to store the values into a vector,everytime one value is being sent,bt the vector only stores the lastest value,nt the entire values//

A: 

I cannot understand the body of your question. But here is some simple code to illustrate how to add values to a Vector.

Vector v = new Vector();
v.add("first string");
v.add("second string");
System.out.println(v.size());  // outputs "2"
System.out.println(v.get(0));  // outputs "first string"
System.out.println(v.get(1));  // outputs "second string"

Unless you have a specific reason for using Vector, it is better to use ArrayList.

Stephen C
Blackberry doesn't support ArrayList.
DaveJohnston
That sounds like a specific reason. (Ugh!!)
Stephen C
I have to work with the Blackberry API almost every day at work and I often find myself saying 'Ugh!!!' ;)
DaveJohnston
Sir DaveJohnston,i hav posted my answer,can u plz help me with the code,i thnk u followed my problem.I hav to store each element frm the array into vector,not only the latest element which i am sending.plz help me with the code sir.
Arunabha Dutta Choudhury
Thnk u sir,vector did work.
Arunabha Dutta Choudhury
Arunabha Dutta Choudhury
@Arunabha - I think that the first paragraph of @DaveJohnston's answer points out what your problem is. You code appears to be creating a new Vector and putting only one element into it.
Stephen C
Arunabha Dutta Choudhury
Arunabha Dutta Choudhury
@Aranabha - That is explained in the second paragraph of @DaveJohnston's answer. His answer says that you first call `getContents()` on the `PersistentObject` and cast the value to a Vector. Then you add data to the `Vector`. And finally, you call `commit()` to update the persistent object.
Stephen C
If you are trying to put more elements into an existing persisted Vector, you should not be doing `new Vector()`. Read @DaveJohnston's answer. Or look at the Restaurant code sample in the "BlackBerry Java Development Environment Development Guide"
Stephen C
Arunabha Dutta Choudhury
@Stephen C-Is there any process by which we can add value to a vector without declaring it as new vector().
Arunabha Dutta Choudhury
@Stephen C- And whenever i try to call get.Contents() earlier to setContents(),i am getting null pointer exception
Arunabha Dutta Choudhury
@Stephen C-whenever i try to declare a vector like dis public static Vector data;instead of the new Vector(),its throwing null pointer expception,cant we add element to a vector without declaring it as new Vector().
Arunabha Dutta Choudhury
@Arunabha - see my other answer. That shows you how to do it. If you still don't understand, I'd advise you to take some lessons in basic Java programming.
Stephen C
A: 

I don't fully understand what you are trying to do, but I can see that you are creating a new Vector every time.

Vector data = new Vector();
data.addElement ( TestScreen.quesNumber + "" );

So of course data is only going to have a single element in it. I am guessing disques is a PersistentObject? So the first thing to do would be to get the existing contents (like you do later on):

synchronized ( disques ) {
    data = ( Vector ) disques.getContents(); 
}

Then add your new content to data and commit it back to the PersistentObject.

DaveJohnston
A: 

Actually i am developing application for blackberry,& it doesnt support ArrayList.Actually i am trying to send value from one page to another (return xyz).Then i am trying to add that value in a vector,& save in into blackberry persistentobject.

synchronized ( disques ) {

  System.out.println ( "value is " + data );
  disques.setContents ( data );
  disques.commit();

} synchronized ( disques ) { data = ( Vector ) disques.getContents(); } So when i trying to store the array value into the vector(my full code is above),only the latest value that i am sending is being stored in the vector,but i want the vector to store all the values,not only the lastest value that is being sent.kindly help me

Arunabha Dutta Choudhury
A: 

package com.firstBooks.series.db;

import java.util.Random;

import net.rim.device.api.system.PersistentObject; import net.rim.device.api.system.PersistentStore; import java.util.Vector;

import net.rim.device.api.util.Arrays; import net.rim.device.api.util.Persistable; import com.firstBooks.series.db.parser.XMLParser; import com.firstBooks.series.ui.managers.TopManager; import com.firstBooks.series.ui.screens.TestScreen; public class DBMain implements Persistable{

public static String answer = "";
public static String selectedAnswer = "";
public static Question curQuestion;

public static int currQuesNumber = 1;
public static int correctAnswerCount = -1;
static int curQuesnew;
static int quesCount=-1;
static int xyz;
static int j=0;
public static int totalNumofQuestions = Question.totques;
public static int quesNum[] = new int[XMLParser.questionList.size()];
static PersistentObject qStore;
static PersistentObject curQues;
static PersistentObject pQues;
static PersistentObject curans;
static PersistentObject disques;
static int a ;
static int b;
static int pques;
static int cans;
static int []dques;
static Vector data = new Vector();

static {
    qStore = PersistentStore.getPersistentObject(0x33010065d24c7883L);
    curQues = PersistentStore.getPersistentObject(0x33010064d24c7883L);
    pQues   = PersistentStore.getPersistentObject(0xbd7460a5b4f9890aL);
    curans  = PersistentStore.getPersistentObject(0xc5c065a3ae1bec21L);
    disques = PersistentStore.getPersistentObject(0x9e6d76868a999451L);

}

static{ 
     initialize();
}


public static void initialize() {

    //int quesNum[] = new int[XMLParser.questionList.size()];
    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++) {
        int randomPosition = rgen.nextInt(quesNum.length);
        int temp = quesNum[i];
        quesNum[i] = quesNum[randomPosition];
        quesNum[randomPosition] = temp;

    }

    synchronized (qStore) {
        qStore.setContents(quesNum);
    qStore.commit();
    }

}


public static int getQuestionNumber() {

        quesCount++;
       synchronized (curans) {

            int b=Integer.parseInt(TopManager.corrCount);
            curans.setContents(b+"");
            curans.commit();

        }
int quesNum[];

    synchronized (qStore) {
        quesNum=(int[]) qStore.getContents();
        System.out.println("The value of question is ...."+quesNum.length);
}


    synchronized (pQues) {

        int a=Integer.parseInt(TopManager.quesNumber);
        pQues.setContents(a+"");
        pQues.commit();

    }


    if (quesNum!=null && quesCount < quesNum.length) {

         data.addElement(TestScreen.quesNumber+"");

        synchronized (curQues) {

            curQuesnew=quesNum[quesCount];
            curQues.setContents(curQuesnew+"");
            curQues.commit();   
        }


        synchronized (disques) {
            data = (Vector)disques.getContents();
               System.out.println("valueee is.........."+data.size());
        }

       synchronized (disques) {
           if (data == null) {


             System.out.println("value is "+data);
            disques.setContents(data);
            disques.commit();
           }
       }


if (data.contains(quesNum[quesCount]+"")){
      initialize();
      System.out.println("i shall overcome.........");
    getQuestionNumber();
        }
  else {
    System.out.println("hardwork conqures all.........");
     xyz=quesNum[quesCount];
     }
   return quesNum[quesCount];


} else {
        initialize();
        quesCount = -1;
        return getQuestionNumber();
    }

}

When i am trying to call getContents() first,& then store it using setContents,my application is nt running,& if i dnt use new vector() at top,then my values are not being saved into the vector,so for that i need to declare new vetor() at top,but when i save the values into persistent store & exit the app,then again restart my app,the vector start frm a new value.I want the vector to hold the previous 10 values,& start frm 11th value,plz help.

Arunabha Dutta Choudhury
-1 - this is not an answer
Stephen C
A: 

This is going to be my last attempt to answer your question. Pay attention.

This is how you should manage your persistent vector.

static Vector data;

static PersistentObject disques = 
    PersistentStore.getPersistentObject(0x9e6d76868a999451L);
}

public static int test() {
    data = (Vector) disques.getContents();
    if (data == null) {
        System.out.println("Disques is empty!!");
        data = new Vector();
        disques.setContents(data);
    } else {
        System.out.println("Disques contains: " + data);
    }
    data.add("This is question " + data.size() + "\n");
    disques.commit();
}

Each time test is called, it fetches the persistent object's value and checks that it has been initialized. If it has not been initialized, it initializes is to an empty Vector. Then it adds one question to the vector an commits the changes.

If you don't understand this, I'm sorry but I'm not prepared to spend any more time trying to explain. And I DEFINITELY am not prepared to spend any time trying to unpick the random mess that your application has turned into.

Stephen C