如何从Android / Kotlin中的Firebase读取子数据的孩子

时间:2019-10-05 14:30:55

标签: android firebase kotlin firebase-realtime-database

我正在尝试将Firebase实时数据库中的数据读取到TextView中。但是每次它在TextView中显示为null时。我想读取单个用户数据。我想在用户输入时读取数据。

我的数据结构看起来像这样。

{
  "Date": {
           "04-10-2019": {
                "-LqR-e2UJLJCccqfgGi1":{
                    address: Dhaka
                     date: 04-10-2019
                     name: Mark
                     phoneNo: 017#######   
                     serialNo: -LqR-e2UJLJCccqfgGi1       
                     type: New
                   },
          },

          "05-10-2019": {
                "-LqU-e2UJLJCDcqfgGi9":{
                    address: Dhaka
                     date: 04-10-2019
                     name: Tony
                     phoneNo: 017#######   
                     serialNo: -LqU-e2UJLJCDcqfgGi9      
                     type: OLd
          },

  }
}

我正在尝试的代码。

class Info(
           val serialNo: String? = "",
           val name: String = "",
           val address: String = "",
           val phoneNo: String = "",
           val date: String = "",
           var type: String = ""

          )

private fun saveInfo() {
        // Edit Text Field
        val name = editTextName.text.toString().trim()
        if (name.isEmpty()) {
            editTextName.error = "Please Enter Your Name"
            return
        }

        val address = editTextAddress.text.toString().trim()
        if (address.isEmpty()) {
            editTextAddress.error = "Please Enter Your Address"
            return
        }

        val phoneNo = editTextPhoneNo.text.toString().trim()
        if (phoneNo.isEmpty()) {
            editTextPhoneNo.error = "Please Enter Your Phone Number"
            return
        }

        val date = dateText.text.toString().trim()

        //Radio Button
        var type = ""
        when {
            checkboxNew.isChecked -> type += "New"
            checkboxOld.isChecked -> type += "Old"
            radioGroup.checkedRadioButtonId <= 0 -> {
                Toast.makeText(applicationContext, "Please Check New or Old", Toast.LENGTH_LONG)
                    .show()
                return
            }
        }

        val current = LocalDateTime.now()
        val formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
        val formatted = current.format(formatter)

        val myRef = FirebaseDatabase.getInstance().getReference("Date").child(formatted)
        myRef.orderByChild(formatted).startAt("28-09-2019").endAt("31-12-2070")

        val patientId = myRef.push().key
        val patient = Info(patientId, name, address, phoneNo, date, type)

        myRef.child((patientId).toString()).setValue(patient).addOnCompleteListener {
            Toast.makeText(applicationContext, "Info saved Successfully", Toast.LENGTH_LONG).show()

            myRef.addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    for (data in dataSnapshot.children) {
                        val user = data.child(formatted).value.toString()
                        dataText.text = user
                    }
                }

                override fun onCancelled(error: DatabaseError) {

                }
            })

        }
    }

我想在用户输入内容并按下按钮时将数据显示在textview中。

1 个答案:

答案 0 :(得分:0)

如果您尝试从数据库中读取date属性,请更改此内容:

val user = data.child(formatted).value.toString()

对此:

val user = data.child("date").value.toString()

您需要在child()内传递属性名称。