Saving Data on Android



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

Crea/ng a QuizRepository
Create a new Kotlin interface under the 
data
 package and name it 
QuizRepository

Add the following code, also importing the missing classes:
interface
 
QuizRepository
 { 
 
  
fun
 
getSavedQuestions
()
: LiveData
 
  
fun
 
saveQuestion
(question: Question)
 
 
  
fun
 
saveAnswer
(answer: Answer)
 
 
  
fun
 
getQuestionAndAllAnswers
()

LiveData
 
  
fun
 
deleteQuestions
()
 
}
The above interface defines the methods that you will need in your repository. By 
using an interface you make it much easier to change the implementation of your 
code if you later decide that you need another database implementation rather than 
Room.
Now, create a new Kotlin class under the 
data
 package and name it 
Repository

Make your class implement the 
QuizRepository
 interface:
class
 
Repository
 : 
QuizRepository {
 
 
}
Override all the functions in the interface to implement them. You can press 
Ctrl + I
 
and Android Studio should display a list of all the missing members. Select all of 
them and press 
OK
.
Next, add the the following properties at the top of your class:
private
 
val
 quizDao: QuizDao 
by
 lazy 
{ QuizApplication.database.questionsDao() } 
private
 
val
 allQuestions 
by
 lazy { quizDao.getAllQuestions() } 
private
 
val
 allQuestionsAndAllAnswers 
by
 lazy 
{ quizDao.getQuestionAndAllAnswers() }
You create two 
LiveData
 values, to observe and react to the data within the 
database, and all the changes. 
allQuestions
 will hold a reference to all the 
questions currently stored in your database.
Saving Data on Android Chapter 9: Using Room with Google's Architecture Components
raywenderlich.com
153


Once again, since this is 
LiveData
, your observers will be notified each time a new 
record is added or updated in the 
questions
 table. 
allQuestionsAndAllAnswers
 
holds a 
LiveData
 list of 
QuestionAndAllAnswers
. This property will be useful when 
displaying a question and its answers to your users.
Now, implement 
saveQuestion()
 and 
saveAnswer()
 like this:
override
 
fun
 
saveQuestion
(question: Question)
 { 
  AsyncTask.execute { quizDao.insert(question) } 

 
override
 
fun
 
saveAnswer
(answer: Answer)
 { 
  AsyncTask.execute { quizDao.insert(answer) } 
}
The above code uses 
insert()
 of your 
quizDao
 to create new 
Question
 and 
Answer
 
records in your database. You are using an  
AsyncTask
 because you don't want to 
execute long-running write operations on the main thread.
Note
: You can also choose different mechanisms to schedule the insert 
operations in the background, such as 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   161   162   163   164   165   166   167   168   ...   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