Saving Data on Android


prepopulate  button, you're going to call  prepopulateQuestions() . If the user taps the  clear



Download 19,28 Mb.
Pdf ko'rish
bet176/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   172   173   174   175   176   177   178   179   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

prepopulate
 button, you're going to call 
prepopulateQuestions()
. If the user taps the 
clear
 button, you will call 
clearQuestions()
.
And that is all you need to do in your 
MainActivity
! Now, you need to set up the 
ViewModel
 for the 
QuestionActivity
.
Saving Data on Android Chapter 9: Using Room with Google's Architecture Components
raywenderlich.com
163


Open 
QuestionActivity.kt
 and add the following property for your 
QuizViewModel
:
private
 
val
 viewModel 
by
 lazy { getViewModel 
{ QuizViewModel(Repository()) } }
Just like 
MainActivity
, you are using lazy initialization and 
getViewModel()
 to 
create an instance of your 
QuizViewModel
 when the 
QuestionActivity
 is first 
created.
Now that your 
ViewModel
 is set up, you only need to create some methods that 
handle the different states provided by your 
QuizState
 and render them to the 
screen. Since all the logic is handled in your 
ViewModel
, this should be pretty easy.
Add the following code:
private
 
fun
 
render
(state: QuizState)
 { 
  
when
 (state) { 
    
is
 QuizState.Empty -> renderEmptyState() 
    
is
 QuizState.Data -> renderDataState(state) 
    
is
 QuizState.Finish -> 
goToResultActivity(state.numberOfQuestions, state.score) 
    
is
 QuizState.Loading -> renderLoadingState() 
  } 
}
render()
 will be in charge of calling the appropriate methods depending on 
QuizState
 using a 
when
 expression.
Now, add the following code to handle your states:
private
 
fun
 
renderDataState
(quizState: QuizState.DataState)
 

//. 1
 
  progressBar.visibility = View.GONE 
  displayQuestionsView() 
  questionsRadioGroup.clearCheck() 
  questionTextView.text = quizState.
data
.question?.text 
  questionsRadioGroup.forEachIndexed { index, view -> 
    
if
 (index < quizState.
data
.answers.size) 
      (view 
as
 RadioButton).text = 
quizState.
data
.answers[index].text 
  } 

 
private
 
fun
 
renderLoadingState
()
 { 
// 2
 
  progressBar.visibility = View.VISIBLE 

 
private
 
fun
 
renderEmptyState
()
 { 
// 3
 
  progressBar.visibility = View.GONE
Saving Data on Android Chapter 9: Using Room with Google's Architecture Components
raywenderlich.com
164


  emptyDroid.visibility = View.VISIBLE 
  emptyTextView.visibility = View.VISIBLE 
}
These are all the methods that 
render()
 will use to display different kinds of screens 
to the user. Taking each commented section in turn:
1.
renderDataState()
 displays a question to your user using
data
 of your 
QuizState.Data
.
2.
renderLoadingState()
 displays a progress bar.
3.
renderEmptyState()
 displays an image and text saying that there are no 
questions in the database.
Now, add the following methods:
fun
 
nextQuestion
(view: View)
 { 
// 1
 
  
val
 radioButton = 
findViewById(questionsRadioGroup.checkedRadioButton
Id) 
  
val
 selectedOption = 
questionsRadioGroup.indexOfChild(radioButton) 
  viewModel.nextQuestion(selectedOption) 

 
private
 
fun
 
displayQuestionsView
()
 { 
// 2
 
  questionsRadioGroup.visibility = View.VISIBLE 
  questionTextView.visibility = View.VISIBLE 
  button.visibility = View.VISIBLE 

 
private
 
fun
 
goToResultActivity
(numberOfQuestions: Int, score: 
Int)
 { 
//. 3
 
  startActivity( 
    intentFor
      SCORE to score, 
      NUMBER_OF_QUESTIONS to numberOfQuestions 
    ).newTask().clearTask() 
  ) 
}
Briefly:
1.
nextQuestion()
 calls your 
ViewModel
's 
nextQuestion()
 passing the index of 
the selected radio button as a parameter. It will be called every time the user taps 
the 
NEXT
 button. The 
OnClickListener
 is bound within the XML to 
nextQuestion()
.
Saving Data on Android Chapter 9: Using Room with Google's Architecture Components
raywenderlich.com
165


2.
displayQuestionsView()
 makes your 
questionsRadioGroup

questionTextView
 and 
button
 widgets visible, so you can display a question to 
your user.
3.
goToResultActivity()
 creates an 
Intent
 to start the 
ResultActivity
 and 
passes the 
score
 and 
numberOfQuestions
 as extras.
Add the following code and import the 
androidx.lifecycle.Observer
:
private
 
fun
 
getQuestionsAndAnswers
()
 { 
  viewModel.getCurrentState().observe(
this
, Observer { 
    render(it) 
  }) 
}
This method simply calls the 
getCurrentState()
 method to get the 
QuizState
 of 
your 
ViewModel
. Since 
QuizState
 is a 
LiveData
 you can observe it using 
observe()
 and each time its value changes you will call 
render()
 passing the new 
QuizState as a parameter.
Finally, add the following line inside 
onCreate()
:
getQuestionsAndAnswers()
Since you immediately want to start observing the 
QuizState

onCreate()
 is the 
best place to call this method.
The last step to finish building your app is to display the quiz results to your user.
Open the 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   172   173   174   175   176   177   178   179   ...   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