인프런 커뮤니티 질문&답변
diet-memo 만들기 : 데이터베이스에 쓰기가 안됩니다.
작성
·
412
0
안녕하세요 선생님 .'
너무 잘 보고 있어요. 질문에도 대답해주셔서 넘 감사합니다.^^
질문 : 앱도 정상적으로 작동하는데 Firebase 실시간 데이터베이스에 연동이 안됩니다.(에러 메서지도 안 뜹니다.)
MainActivity.kt
package com.ipari.diet_memo import android.app.DatePickerDialog import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.widget.Button import android.widget.DatePicker import android.widget.EditText import android.widget.ImageView import androidx.appcompat.app.AlertDialog import com.google.firebase.database.ktx.database import com.google.firebase.ktx.Firebase import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val writeButton = findViewById<ImageView>(R.id.writeBtn) writeButton.setOnClickListener { val mDialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog, null) val mBuilder = AlertDialog.Builder(this) .setView(mDialogView) .setTitle("운동 메모 다이얼로그") val mAlertDialog = mBuilder.show() val DateSelectBtn = mAlertDialog.findViewById<Button>(R.id.dataSelectBtn) var dateText="" DateSelectBtn?.setOnClickListener { val today = GregorianCalendar() val year : Int = today.get(Calendar.YEAR) val month : Int = today.get(Calendar.MONTH) val date : Int = today.get(Calendar.DATE) val dlg = DatePickerDialog(this, object : DatePickerDialog.OnDateSetListener{ override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int ) { Log.d("MAIN", "${year}, ${month +1}, ${dayOfMonth}") DateSelectBtn.setText("${year}, ${month +1}, ${dayOfMonth}") dateText = "${year}, ${month +1}, ${dayOfMonth}" } }, year, month, date) dlg.show() } val saveBtn = mAlertDialog.findViewById<Button>(R.id.saveBtn) saveBtn?.setOnClickListener { val healthMemo = mAlertDialog.findViewById<EditText>(R.id.healthMemo)?.text.toString() val database = Firebase.database val myRef = database.getReference("myMemo") val model = DataModel(dateText, healthMemo) myRef .push() .setValue(model) } } } }
Gradle file
plugins { id 'com.android.application' id 'com.google.gms.google-services' id 'kotlin-android' } android { compileSdk 33 defaultConfig { applicationId "com.ipari.diet_memo" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation platform('com.google.firebase:firebase-bom:30.4.1') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-auth-ktx' implementation 'com.google.firebase:firebase-database-ktx' }
custom_dialog. xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/healthMemo" android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="20dp" android:hint="운동메모를 입력해주세요." android:background="@android:color/transparent"/> <LinearLayout android:layout_width="match_parent" android:background="#999999" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_height="0.5dp"/> <Button android:id="@+id/dataSelectBtn" android:text = "날짜를 선택해주세요." android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="60dp"/> <Button android:id="@+id/saveBtn" android:text = "저장하기" android:textColor="@color/black" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="match_parent" android:layout_height="60dp"/> </LinearLayout>
_____그리고 다른 분들은 코드 올릴 때 텍스트형식이 아닌 일정한 형식으로 올리시던데 어떻게 하는건가요? ㅎㅎ
퀴즈
컴퓨팅 시스템에서 서버의 주된 역할은 무엇인가요?
사용자 인터페이스를 디자인하는 역할
클라이언트의 요청에 따라 데이터를 제공하는 역할
앱의 모든 코드를 최종적으로 실행하는 역할
클라이언트 기기의 하드웨어를 직접 제어하는 역할
답변 2
0
다시 처음부터 하고있는데
첫번째 그래들에서 문제가 발생한것 같습니다
<firebases sdk추가>

<안드로이드 스튜디오><build.gradle>(project:diet)
이렇게 되어있었고
이렇게 allprojects부터 추가를 하니
google() 이 오류가 떠서 주석처리로 해결하니
'mavenrepo' 이 다시 오류가 떳습니다
찾아보니[안드로이드 에러] 라이브러리 추가 시 에러 (Build was configured to prefer settings repositories..) 해결 (tistory.com)
원인 : 글을 작성하는 시점의 최신 안드로이드 스튜디오 버전인 Arctic Fox(2020.3.1) 이후 버전은 라이브러리 추가를 위해 allprojects { } 코드 사용 시 에러가 발생 한다고 합니다
질문 1. 이렇게 해결 과정이 있었는데 제가 너무 왕초보라 어떻게 적용을 해야할지 잘 모르겠네요..
질문 2. 더 낮은 버젼으로 다운받으려고 하니 폭스 버전 모두 (2020.3.1)인데 어떤걸 다운 받아야 하는지 모르겠습니다
<settings.gradle>

다운로드 받은 settings.grdle 파일에는

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}이 부분이 없어서 지워봤더니 에러는 뜨지 않았습니다
https://www.youtube.com/watch?v=WKD2yy0Juag
강의와 동일하게 진행하려면
위의 영상을 참고해서 다운받아보시겠어요?
더 낮은 버젼으로 다운받으려고 하니 폭스 버전 모두 (2020.3.1)인데 어떤걸 다운 받아야 하는지 모르겠습니다
이 부분은 아무거나 다운받으셔도 무관합니다.
잘 안되시면 전체 코드를 깃허브에 올리시고 공유해주시면 살펴볼게요~
0







혹시 어떻게 진행하셨나요?
좀 더 자세히 처음부터 알려주시겠어요?