Retrieving Parsing and Storing Trivia Question Data

When the Been There, Done That! application runs out of questions to display to the user, it attempts to retrieve a new batch of questions. This architecture makes enabling networking for the application more straightforward in future hours because the parsing of the XML remains the same.

Each batch of questions arrives as a simple XML file, which needs to be parsed. You can store the current batch of questions in memory by using a simple but powerful data structure—in this case, a Hashtable member variable.

Declaring Helpful String Literals for Question Parsing

Take a moment to review the XML format used by the question batches, discussed earlier. To parse the question batches, you need to add several String literals to represent the XML tags and attributes to the class:

public static final String XML_TAG_QUESTION_BLOCK = "questions";

public static final String XML_TAG_QUESTION = "question";

public static final String XML_TAG_QUESTION_ATTRIBUTE_NUMBER = "number";

public static final String XML_TAG_QUESTION_ATTRIBUTE_TEXT = "text";

public static final String XML_TAG_QUESTION_ATTRIBUTE_IMAGEURL = "imageUrl";

While you are at it, you can also define the default batch size, to simplify allocation of storage for questions while parsing the XML: public static final int QUESTION_BATCH_SIZE = 15;

Storing the Current Batch of Questions in a Hashtable

Now within the QuizGameActivity class, you can implement a simple helper class called Question to encapsulate each trivia question:

private class Question { int mNumber; String mText; String mImageUrl;

public Question(int questionNum, String questionText, String questionImageUrl) { mNumber = questionNum; mText = questionText;

mImageUrl = questionlmageUrl;

You will not be storing all the questions locally. Instead, you fetch a batch of questions at a time (for now, from local mock XML files, later from the web). You need a place to store these questions, so declare a Hashtable member variable within the QuizGameActivity class to hold a batch Question objects in memory after you have parsed a batch of XML: Hashtable<Integer, Question> mQuestions;

Did you Know?

You can instantiate the Hashtable member variable in the onCreate() method of the QuizGameActivity class as follows:

mQuestions = new Hashtable<Integer, Question>(QUESTION_BATCH_SIZE);

Now, assuming we have some XML representing a new batch of questions, we can create an XmlResourceParser object called questionBatch. The XmlResourceParser can be used it to extract the data for each question and save into a Hashtable member variable of type Question, using the put() method, like this:

String questionNumber =

questionBatch.getAttributeValue(null Integer questionNum =

new Integer(questionNumber); String questionText =

questionBatch.getAttributeValue(null String questionImageUrl =


// Save data to our hashtable mQuestions.put(questionNum, new Question(questionNum, questionText, questionImageUrl));

You can check for the existence of a specific question in the Hashtable member variable by question number, using the containsKey() method. You can also retrieve a specific Question object by its question number by using the get() method:

Question curQuestion = (Question) mQuestions.get(questionNumber);

The Android SDK includes many commonly used Java classes. For example, you'll find many familiar data structures (such as Hashtable) and utility classes in the java.util package, as well as additional specialized classes in the android.util package.



0 0

Post a comment