inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[중급편] 친절한 JETPACK 개론 <상> (Android Kotlin)

LiveData 와 MutableLiveData 차이

LiveData 와 MutableLiveData 차이강의중에서

502

shafeel2

작성한 질문수 67

0

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

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

 

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

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

Type mismatch: inferred type is MainViewModel but ViewModel was expected

android kotlin jetpack

답변 5

0

shafeel2

해당코드를

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

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

캄사합니다

0

shafeel2

github 주소 올려두었습니다

0

shafeel2

github 주소입니당

https://github.com/dongguntechnology/ViewModelMutableLiveData

0

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

 

1

개복치개발자

안녕하세요

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

0

shafeel2

class MainViewModel {
private var testMutableLiveData = MutableLiveData(0)

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

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


}

dataBinding 시 오류

0

66

2

Retrfit 에러

0

146

2

[Retrofit 간단한 예제 변경 + RecyclerView 추가] 에 databinding 추가해 보았습니다.

1

218

2

Navigation Data Transfer Safe Args 강의중에서

0

267

2

viewModel 초기화 관련 질문드려요

0

283

1

Fragment에서 ViewBinding과 DataBinding 초기화 차이가 궁금합니다.

0

282

1

최상단 변수 선언 관련하여 질문드려요

0

189

1

Retrofit + ViewModelScope + RecyclerView + Glide 강의중에서

0

321

2

Retrofit 간단한 예제 변경 강의중에서

0

301

2

Activity/Fragment ViewModel 공유 10분35초경에 fragment 에 viewModel선언

0

221

1

안드로이드 테마 색상 오류

0

245

1

Map / SwitchMap - 2 강의 오류

0

386

1

에러

0

351

1

ViewBinding 질문 있습니다.

2

386

2

fragment 이동시 data 초기화

0

391

1

CoroutineScope 질문

0

371

1

뷰모델 질문

0

308

1

바인딩 때 xml

0

289

1

코루틴과 workmanager 순서 정하기의 차이점

0

521

1

ViewModel 값

0

296

1

Retrofit 질문입니다.

0

491

1

Activity / Fragment 의 ViewMode 질문있습니다.

0

317

1

2강 질문이씁니다.

0

283

1

Json 형식 데이터 모델 설계 질문드립니다 :)

2

1518

1