Lab 10 - Store data persistently in a web database (1)
Lab 10 - Store data persistently in a web database (1)
You’ll build a MakeQuiz app that lets a “teacher” create quizzes using an input form. The quiz
questions and answers will be stored in a web database so that “students” can access a
separate TakeQuiz app and take the test.
i. Getting started
Start a new project. Name it “MakeQuiz” and set the screen’s title to “Make Quiz”.
Use the Component Designer to create the interface for MakeQuiz. When you finish, it
should look something like figure below:
1
CIT4173 Mobile Application Development LAB 10
o From Variables, drag two initialize global name blocks, and change the
name to QuestionList and AnswerList. The lists are defined with the
create empty list block.
o Drag another initialize global name block, and change the name to
answerIndex and set the initialize value to 1.
o Drag another initialize global name block, and change the name to
answer and set the initialize value to empty text.
The first behavior you’ll build is for handling the user’s input. Specifically, when the user
enters a question and answer and clicks Submit, you’ll use add items to list blocks to
update the QuestionList and AnswerList.
2
CIT4173 Mobile Application Development LAB 10
When a user submits a question-answer pair, you’ll want to clear the Question Text and
AnswerText text boxes so that they’re ready for a new entry instead of showing the
previous one.
In the app you’ve built so far, the question and answer lists are displayed separately and
using the default list display format for App Inventor. So if you were making a quiz on state
capitals and had entered two pairs of questions and answers, it might appear as:
(What is the capital of Malaysia? How many stripes are there on Malaysian Flag ?: Kuala
Lumpur 14)
This is obviously not an ideal user interface for the quiz designer. A better display would
show each question along with its corresponding answer, with one question-answer pair
per line, like this:
What is the capital of Malaysia?: Kuala Lumpur
How many stripes are there on Malaysian Flag?: 14
3
CIT4173 Mobile Application Development LAB 10
The task here is a bit more complicated because you’re dealing with two lists. Because of
its complexity, you’ll put the blocks for displaying the data in a procedure named
displayQAs, and call that procedure from the SubmitButton.Click event handler.
To display question-answer pairs on separate lines, you’ll need to do the following:
• Use a for each block to iterate through each question in the QuestionList.
• Use a variable answerIndex so that you can grab each answer as you iterate through the
questions.
• Use join to build a text object with each question and answer pair, and a newline
character (\n) separating each pair.
The ‘for each’ only allows you to iterate through a single list. In this case, there are two lists,
QuestionList and AnswerList. The for each is used to iterate through the QuestionList, but you
need to select an answer, as well, as you proceed through the questions.
To accomplish this, you use an index variable. In this case, the index variable, answerIndex, is
used to track the position in the AnswerList as the for each goes through the QuestionList.
answerIndex is set to 1 before the for each begins. Within the for each, answerIndex is used
to select the current answer from the AnswerList, and then it is incremented.
On each iteration of the for each, the current question and answer are concatenated to the
end of the QuestionsAnswersLabel.Text property, with a colon between them.
4
CIT4173 Mobile Application Development LAB 10
You now have a procedure for displaying the question-answer pairs, but it won’t help
unless you call it when you need it. Modify the SubmitButton.Click event handler by calling
displayQAs instead of displaying the lists, as was done previously. The updated blocks
should appear as shown in Figure below:
Here’s the general scheme for making list data (such as the questions and answers for
our app) persistent:
• Store a list to the database each time a new item is added to it.
• When the app launches, load the list from the database into a variable.
Start by storing the QuestionList and AnswerList in the database each time the user enters
a new pair.
5
CIT4173 Mobile Application Development LAB 10
6
CIT4173 Mobile Application Development LAB 10
The valueFromWebDB argument of GotValue holds the data returned from the database
request. You need the outer if block in the event handler because the database will return
an empty text (“”) in valueFromWebDB if it’s the first time the app has been used and
there aren’t yet questions and answers. By asking if the valueFromWebDB is a list?,
you’re making sure there is some data actually returned. If there isn’t any data, you’ll bypass
the blocks for processing it.
If data is returned (is a list? is true), the blocks go on to check which request has arrived. The
tag identifying the data is in tagFromWebDB: it will be either “yournamequestions” or
“yournameanswers.” If the tag is “yournamequestions,” the valueFromWebDB is put into
the variable QuestionList. Otherwise (else), it is placed in the AnswerList.
You only want to display the lists after both have arrived (GotValue has been triggered
twice). Can you think of how you’d know for sure that you have both lists loaded in from the
database? The blocks shown use an if test to check whether the lengths of the lists are the
same, as this can only be true if both lists have been returned. If they are, the handy
displayQAs procedure you wrote earlier is called to display the loaded data.
Are you done? Wow, congratulations! You’re truly on your way to being an
amazing app designer.