강의

멘토링

커뮤니티

Inflearn コミュニティ Q&A

shafeel22040 のプロフィール画像
shafeel22040

投稿した質問数

[中級編] Kind JETPACK 概論 <上> (Android Kotlin)

LiveData 와 MutableLiveData の 違い

LiveData 와 MutableLiveData 차이강의중에서

作成

·

468

0

늘 수고많으신 개복치강사님

24년 갑진년에도 건강하시고 부자되세여

 

viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

위 코드에서 아래와 같은 오류가 발생하네요

Type mismatch: inferred type is MainViewModel but ViewModel was expected

androidkotlinjetpack

回答 5

0

shafeel2님의 프로필 이미지
shafeel2
質問者

해당코드를

viewModel = ViewModelProvider(this)[MainViewModel::class.java]

로 변경하니 오류가 사라지네요

캄사합니다

0

shafeel2님의 프로필 이미지
shafeel2
質問者

github 주소 올려두었습니다

0

shafeel2님의 프로필 이미지
shafeel2
質問者

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()
})
}
}

 

bokchi님의 프로필 이미지
bokchi
インストラクター

안녕하세요

전체 프로젝트 코드를 압축해서 구글 드라이브 등에 올려놓고 링크를 공유해주시겠어요?

0

shafeel2님의 프로필 이미지
shafeel2
質問者

class MainViewModel {
private var testMutableLiveData = MutableLiveData(0)

val testLiveData : LiveData<Int> get() =
testMutableLiveData

fun plusLiveDataValue(){
_testMutableLiveData.value = _testMutableLiveData.value!!.plus(1)
}


}

shafeel22040 のプロフィール画像
shafeel22040

投稿した質問数

質問する