inflearn logo
강의

Course

Instructor

Modern Android - Utilizing Kotlin and Jetpack

[Kotlin] How to store data using DB Room

Room db kotlin 강의중에서

421

작성자 없음

0 asked

0

강사님 늘 건강하시고 부자되세요

 

아래 activity_main.xml 내의 요소들과 관련된 부분에서 빨간줄이 생깁니다 왜 그런지 .....

q1.png

android kotlin jetpack

Answer 3

0

shafeel2

일부오류는 사라졌으나

아래부분에서 오류가 발생합니다

addButton.setOnClickListener(
db.todoDao().insert(Todo(todoEdit.text.toString()))
resultText.text = db.todoDao().getAll().toString()
)

 

q2.png

0

survivalcoding

TextView의 경우에 text 에 값을 지정하는 것이 안 되고 setText() 함수를 통해야 할 거에요.

resultText.setText(db.todoDao().getAll().toString()) 으로 고쳐보시겠어요?

위쪽 라인에 같은 코드도 빨간불은 없지만 아마 동일하게 수정하셔야 할 것 같습니다.

0

survivalcoding

지금은 자동으로 XML 의 id를 가져오지 못 합니다.

따라서 Java 처럼 findViewById() 를 쓰거나, ViewBinding 이라는 걸 써야 합니다.

findViewById() 를 사용하신다면 add_button 대신에 다음과 같이 고치거나

findViewById<Button>(R.id.add_button)

사용 전에 타입에 맞도록 가져와서 미리 할당해 두어도 됩니다. (setContentView() 이후에)

val addButton = findViewById<Button>(R.id.add_button)
val todoEdit = findViewById<EditText>(R.id.todo_edit)
...

 

다른 방식으로는 ViewBinding 이라는 것이 있습니다.

findViewById() 대신에 미리 정해둔 설정을 하면 자동으로 가져다 쓸 수 있도록 하는 설정입니다.

의 가이드는 https://developer.android.com/topic/libraries/view-binding?hl=ko#kts 있습니다.

저는 ViewBinding을 선호합니다.

0

shafeel2

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/todo_edit"
android:hint="활동메모"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@android:color/transparent"
tools:ignore="MissingConstraints" />


<TextView
android:id="@+id/result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/add_button"
android:text="추가"
android:textColor="@color/white"
android:background="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="160dp"
android:layout_marginTop="256dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

Room_DB코틀린 버전에서 에러 나시는 분들

1

294

1

Room db kotlin 관련질문

0

282

2

공적 마스크 조회 앱 자바 버전 질문입니다.

0

388

1

RecyclerView Adapter에서 ViewModel 접근에 관하여 질문 드립니다.

0

1401

1

네비게이션 그래프

0

229

1

Repository에서 Error처리를 하고 싶은데 잘 모르겠습니다.

0

755

1

getAll을 직접부를 때 무한무프 질문있습니다.

0

209

1

라이브 데이터 + 데이터 바인딩 질문드립니다!

0

219

1

/?m=5000 에 대해서 질문드립니다.

0

228

2

@Inject lateinit var repository: MyRepository

0

286

1

이번편 강의의 3:52 부분까지 공부했는데 에러가 걸립니다

0

412

2

let이나 apply는 주로 어떤 상황에 사용하나요?

0

262

2

Call<StroeInfo> 를 StoreInfo로 바꾸는 부분 질문

0

240

1

Room DB 파일을 확인하고 싶어요.

0

2338

1

SavedStateHandle 앱 종료 시킨 후, 다시 열면 0으로 초기화 되어있습니다ㅠ

0

476

1

안녕하세요.

0

442

4

NullPointerException이 뜨는데 이것도 마스크 재고 API 중지 때문인가요

0

287

1

viewModel 질문 있습니다.

0

390

1

suspend

0

241

2

retrofit2에서 코루틴을 이용하는 이유가 무엇인가요?

1

275

2

UpdateItems 생성

0

149

1

질문있습니다.

1

235

3

질문있습니다!

0

269

1

리사이클러뷰 어댑터에서 뷰모델

0

237

1