Saving Data on Android


drodquiz  with the name  viewmodel



Download 19,28 Mb.
Pdf ko'rish
bet169/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   165   166   167   168   169   170   171   172   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

drodquiz
 with the name 
viewmodel
. Create a new 
Kotlin class inside this package and name it 
MainViewModel
.
Make 
MainViewModel
 extend the 
ViewModel
 by modifying your code like this:
class
 
MainViewModel
() : ViewModel() { 
     
}
Now, since you are going to need your repository to interact with your database from 
your 
ViewModel
s, add the following parameter to the primary constructor of your 
class:
class
 
MainViewModel
(
private
 
val
 repository: QuizRepository) : 
ViewModel() { 
     
}
Finally, add the following code:
fun
 
prepopulateQuestions
()
 { 
  
for
 (question 
in
 QuestionInfoProvider.questionList) { 
    repository.saveQuestion(question) 
  } 
  
for
 (answer 
in
 QuestionInfoProvider.answerList) { 
    repository.saveAnswer(answer) 
  } 

 
fun
 
clearQuestions
()
 = repository.deleteQuestions()
prepopulateQuestions()
 creates new question and answer records in your 
database by using the sample data included in your 
QuestionInfoProvider
, while 
clearQuestions()
 deletes all the questions in your database using 
deleteQuestions()
 on your 
QuizRepository
.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   165   166   167   168   169   170   171   172   ...   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