-
카테고리
-
세부 분야
모바일 앱 개발
-
해결 여부
미해결
LiveData 와 MutableLiveData 차이강의중에서
24.03.07 19:44 작성 조회수 189
0
늘 수고많으신 개복치강사님
24년 갑진년에도 건강하시고 부자되세여
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
위 코드에서 아래와 같은 오류가 발생하네요
Type mismatch: inferred type is MainViewModel but ViewModel was expected
답변을 작성해보세요.
0
0
0
0
shafeel2
질문자2024.03.07
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
findViewById<Button>(R.id.btnArea).setOnClickListener {
viewModel.plusLiveDataValue()
}
viewModel.testLiveData.observe(this, Observer {
findViewById<TextView>(R.id.textArea).text = it.toString()
})
}
}
0
shafeel2
질문자2024.03.07
class MainViewModel {
private var testMutableLiveData = MutableLiveData(0)
val testLiveData : LiveData<Int> get() = testMutableLiveData
fun plusLiveDataValue(){
_testMutableLiveData.value = _testMutableLiveData.value!!.plus(1)
}
}
답변 5