Crea/ng a provider class
Now that your DAO methods are ready, you will create a provider class, that you will
need later on, to prepopulate your database. Create a new class under the
data
package, name it
QuestionInfoProvider
and add the following method:
private
fun
initQuestionList
()
: MutableList {
val
questions = mutableListOf()
questions.add(
Question(
1
,
"Which of the following languages is not commonly used
to develop Android Apps"
)
)
questions.add(
Question(
2
,
"What is the meaning of life?"
)
)
return
questions
}
This method creates a
MutableList
of two questions with the respective
id
and
text
fields. You will use these questions later when prepopulating your database.
But you will also need to have answers for the above questions, so add the following
method:
private
fun
initAnswersList
()
: MutableList {
val
answers = mutableListOf()
answers.add(Answer(
1
,
1
,
true
,
"Java"
))
answers.add(Answer(
2
,
1
,
false
,
"Kotlin"
))
answers.add(Answer(
3
,
1
,
false
,
"Ruby"
))
answers.add(Answer(
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
137
4
,
2
,
true
,
"42"
))
answers.add(Answer(
5
,
2
,
false
,
"35"
))
answers.add(Answer(
6
,
2
,
false
,
"7"
))
return
answers
}
Just like
initQuestionsList()
, this method is creating a
MutableList
, but with
Answer
objects. Each answer corresponds to a
Question
of the previous method,
using the
questionId
property to match each of them.
Now, add the following properties at the top of your class:
var
questionList = initQuestionList()
var
answerList = initAnswersList()
The above properties are immediately initialized with the list of questions and
answers that
initQuestionList()
and
initAnswersList()
return.
Finally, make this class a singleton by changing the
class
keyword to
object
. Your
code should now look like this:
object
QuestionInfoProvider {
var
questionList = initQuestionList()
var
answerList = initAnswersList()
private
fun
initQuestionList
()
: MutableList {
val
questions = mutableListOf()
questions.add(
Question(
1
,
"Which of the following languages is not commonly used
to develop Android Apps"
))
questions.add(
Question(
2
,
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
138
"What is the meaning of life?"
))
return
questions
}
private
fun
initAnswersList
()
: MutableList {
val
answers = mutableListOf()
answers.add(Answer(
1
,
1
,
true
,
"Java"
))
answers.add(Answer(
2
,
1
,
false
,
"Kotlin"
))
answers.add(Answer(
3
,
1
,
false
,
"Ruby"
))
answers.add(Answer(
4
,
2
,
true
,
"42"
))
answers.add(Answer(
5
,
2
,
false
,
"35"
))
answers.add(Answer(
6
,
2
,
false
,
"7"
))
return
answers
}
}
The reason you use the object keyword is to make this class a singleton so that we
never really have to instantiate this class ourselves since you are only interested in
the utilities that this class provides.
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
139
And that's it!
Build
and
Run
your app to verify that it is still working as expected:
Sweet! Although the UI is still not working, your database and DAOs are pretty much
ready to use. You'll connect the business logic in the following chapters.
Tes/ng your database
Although your app's UI is not working yet, you can still interact with your database
by performing some tests such as adding or deleting questions to verify its
functionality.
Now, to test your database, you could start writing code in your activities that inserts
or deletes data and prints the results. You could also wait until you have all your
ViewModels
and wire it to your UI. The problem with this approach is that you might
end up with a ton of code that you will have to delete at the end anyway. Also, you
might forget to delete some of your
print()
statements and expose sensitive data
from your users.
To avoid the above issues, you are going to use a very useful testing framework for
Andriod called
Espresso
.
Saving Data on Android
Chapter 8: The DAO Pattern
raywenderlich.com
140
In your project, there's a
Do'stlaringiz bilan baham: |