강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của shafeel22040
shafeel22040

câu hỏi đã được viết

[Trung cấp] Tổng quan Kind JETPACK <Tập 1> (Android Kotlin)

Sự khác biệt giữa LiveData và MutableLiveData

LiveData 와 MutableLiveData 차이강의중에서

Viết

·

485

0

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

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

 

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

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

Type mismatch: inferred type is MainViewModel but ViewModel was expected

androidkotlinjetpack

Câu trả lời 5

0

shafeel2님의 프로필 이미지
shafeel2
Người đặt câu hỏi

해당코드를

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

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

캄사합니다

0

shafeel2님의 프로필 이미지
shafeel2
Người đặt câu hỏi

github 주소 올려두었습니다

0

shafeel2님의 프로필 이미지
shafeel2
Người đặt câu hỏi

0

shafeel2님의 프로필 이미지
shafeel2
Người đặt câu hỏi

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
Người chia sẻ kiến thức

안녕하세요

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

0

shafeel2님의 프로필 이미지
shafeel2
Người đặt câu hỏi

class MainViewModel {
private var testMutableLiveData = MutableLiveData(0)

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

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


}

Hình ảnh hồ sơ của shafeel22040
shafeel22040

câu hỏi đã được viết

Đặt câu hỏi