작성
·
932
0
질문 : 파이어베이스 리얼타임 데이터베이스에 날짜와 메모가 입력이 잘되었는데 어플 창에는 날짜가 뜨지 않습니다.
-adapter_list.notifyDataSetChanged()하기 전까지는 날짜가 잘 떳는데 그 다음부턴 날짜가 파이어베이스에만 입력되고 어플 창에는 뜨지 않습니다 ㅜㅜ
왜이런걸까요?
-파이어베이스 리얼타임 데이터베이스에 입력이 잘됨
문제 화면: 어플 화면에 날짜가 안뜸
-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.*
import androidx.appcompat.app.AlertDialog
import com.google.firebase.auth.ktx.auth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase
import java.util.*
class MainActivity : AppCompatActivity() {
val dataModelList = mutableListOf<DataModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val database = Firebase.database
val myRef = database.getReference("myMemo")
val listView = findViewById<ListView>(R.id.mainLV)
val adapter_list = ListViewAdapter(dataModelList)
listView.adapter= adapter_list
Log.d("DataModel------", dataModelList.toString())
myRef.child(Firebase.auth.currentUser!!.uid).addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
dataModelList.clear()
for (dataModel in snapshot.children) {
Log.d("Data", dataModel.toString())
dataModelList.add(dataModel.getValue(DataModel::class.java)!!)
}
adapter_list.notifyDataSetChanged()
Log.d("DataModel", dataModelList.toString())
}
override fun onCancelled(error: DatabaseError) {
}
})
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").child(Firebase.auth.currentUser!!.uid)
val model = DataModel(dateText, healthMemo)
myRef
.push()
.setValue(model)
mAlertDialog.dismiss()
}
}
}
}
저도 같은 증상인데, adapter 코드는 아래와 같습니다.
뭐가 문제일까요?
package com.sunny.diet_memo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
class ListViewAdapter(val List : MutableList<DataModel>) : BaseAdapter(){
override fun getCount(): Int {
return List.size
}
override fun getItem(position: Int): Any {
return List[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var convertView =convertView
if(convertView == null){
convertView = LayoutInflater.from(parent?.context).inflate(R.layout.listview_item, parent, false)
}
val date = convertView?.findViewById<TextView>(R.id. listViewDateArea)
val memo = convertView?.findViewById<TextView>(R.id. listViewMemoArea)
date!!.text = List[position].date
date!!.text = List[position].memo
return convertView!!
}
}