google-developer-training / android-basics-kotlin-unscramble-app

Apache License 2.0
89 stars 256 forks source link

Please Help Stuck in this codelab, Score Is not Updating in the Unscramble app! #146

Closed 0-BlackSpectrum-0 closed 8 months ago

0-BlackSpectrum-0 commented 2 years ago

https://user-images.githubusercontent.com/79910450/149629723-511c1ce8-b590-4d64-8f47-b7dbfb5cca00.mp4

URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-training-livedata?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-3-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-livedata#5

In which task and step of the codelab can this issue be found? Running App and Viewing LiveData.

Describe the problem Score is not updating in the app, but word count is updating no problem, I've attached a video to the issue please help!

Steps to reproduce?

  1. Go to... Don't know
  2. Click on...I've followed the code lab
  3. See error...No live data for Score

Versions Android Studio version: Arctic Fox | 2020.3.1 Patch 4 API version of the emulator: 31

Additional information Include screenshots if they would be useful in clarifying the problem. Here's the code:

GameFragment.kt `/*

package com.example.android.unscramble.ui.game

import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import com.example.android.unscramble.R import com.example.android.unscramble.databinding.GameFragmentBinding import com.google.android.material.dialog.MaterialAlertDialogBuilder

/**

`package com.example.android.unscramble.ui.game

import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel

class GameViewModel : ViewModel() { private val _score = MutableLiveData(0) val score: LiveData get() = _score

private val _currentWordCount = MutableLiveData(0)
val currentWordCount: LiveData<Int>
   get() = _currentWordCount

private val _currentScrambledWord = MutableLiveData<String>()
val currentScrambledWord:  LiveData<String>
   get() = _currentScrambledWord

private var wordsList: MutableList<String> = mutableListOf()
private lateinit var currentWord: String

init {
    Log.d("Game Fragment" , "GameViewModel Created!")
    getNextWord()
}

override fun onCleared() {
    super.onCleared()
    Log.d("GameFragment", "GameViewModel Destroyed!")
}

private fun getNextWord(){
    currentWord = allWordsList.random()
    val tempWord = currentWord.toCharArray()
    tempWord.shuffle()

    while (String(tempWord).equals(currentWord, false)) {
        tempWord.shuffle()
    }
    if (wordsList.contains(currentWord)){
        getNextWord()
    } else {
        _currentScrambledWord.value = String(tempWord)
        _currentWordCount.value = (_currentWordCount.value)?.inc()
        wordsList.add(currentWord)
    }
}

fun nextWord():Boolean{
    return if(_currentWordCount.value!! < MAX_NO_OF_WORDS){
        getNextWord()
        true
    }else false

}

private fun increaseScore(){
    _score.value?.plus(SCORE_INCREASE)
}

fun isUserWordCorrect(playerWord: String): Boolean{
    if(playerWord.equals(currentWord,true)){
        increaseScore()
        return true
    }
    return false
}

fun reinitializeData(){
    _score.value=0
    _currentWordCount.value=0
    wordsList.clear()
    getNextWord()
}

} `

lufengdie commented 2 years ago

As the tutorial of Codelab said, the score update was not included in it, we can implement it by ourselves.

image