views:

36

answers:

0

Possible Duplicate:
How to compare values from persistentObject in blackberry

I am developing an application for blackberry.It is a practice test.It has got two version FULL & LITE.The full version has gt 350 question in the xml database,while lite version contains only 20.When a test starts for full version it displays 50 questions b4 a test is completed,while for lite version only 10 questions r being displayed.The questions are being generated randomly & uniquely.Nw i want to add a feature,so dat for Full version & LITE version,if the user exits after taking one test,& den after sometime again he come to take another test,so this time the questions that were displayed in d earlier test wont be displayed again.I want to first exhaust all the questions in the database,without any repeatation.That means a person after appearing for 7 tests(total question 350,per test comprises of 50 questions) can only find the same question in the 8th test.So for this i want to store the questions into the flash memory of blackberry,so that if a user exits also after appearing for a particular test,or he resets the battery,& then later comes back to give test again,then the values stored in the flash memory will match if the questions displyed dis time already exists in the memory or not.If it exists it should display some other question to the user.So can anyone help me how to store the questions in the flash memory,& what should be the logic to verify whether the question already exist in the memory.I knw it will b implemented using persistentobject,& i hav used it also,bt may be my implementation or logic is wrong.I am providing the reqd pages,XMLParser & DBMain,XMLParser parses the questions from the xml database,while DBMain generates the questions randomly,plz help me any1.

XMLParser Page

package com.firstBooks.series7.db.parser;

import java.io.IOException; import java.io.InputStream; import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilder; import net.rim.device.api.xml.parsers.DocumentBuilderFactory; import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException;

import com.firstBooks.series7.AppMain; import com.firstBooks.series7.db.Question;

public class XMLParser {

private Document document;
public static Vector questionList;

public XMLParser() {                 
    questionList = new Vector();
}

public void parseXMl() throws SAXException, IOException,
        ParserConfigurationException {

    // Build a document based on the XML file.
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream inputStream = getClass().getResourceAsStream(AppMain._xmlFileName);

    document = builder.parse(inputStream);
}

public void parseDocument() {
    Element element = document.getDocumentElement();

    NodeList nl = element.getElementsByTagName("question");

    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            Element ele = (Element) nl.item(i);
            Question question = getQuestions(ele);
            questionList.addElement(question);
        }
    }
}

private Question getQuestions(Element element) {

    String title = getTextValue(element, "title");
    String choice1 = getTextValue(element, "choice1");
    String choice2 = getTextValue(element, "choice2");
    String choice3 = getTextValue(element, "choice3");
    String choice4 = getTextValue(element, "choice4");
    String answer = getTextValue(element, "answer");
    String rationale = getTextValue(element, "rationale");

    Question Questions = new Question(title, choice1,
            choice2, choice3, choice4, answer, rationale);

    return Questions;
}

private String getTextValue(Element ele, String tagName) {
    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

}

DBMain Page

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 = AppMain._totalNumofQuestions ;

static int quesNum[] = new int[XMLParser.questionList.size()];
static int quesCount = -1;
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++) {
        int randomPosition = i + rgen.nextInt(quesNum.length-i);
        int temp = quesNum[i];
        quesNum[i] = quesNum[randomPosition];
        quesNum[randomPosition] = temp;
        System.out.println("The value of QUESNUM is"+quesNum);

    }

    synchronized(store) {
        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){
        return quesNum[quesCount];
     }
    else{ 
       initialize();
       quesCount = -1;
       return getQuestionNumber();
    }
}

}