Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet151/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   147   148   149   150   151   152   153   154   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

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 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   147   148   149   150   151   152   153   154   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish