【问题标题】:Firebase Realtime Database - ChildEventListener - BasicsFirebase 实时数据库 - ChildEventListener - 基础
【发布时间】:2020-04-16 20:18:16
【问题描述】:

我创建了一个将用户添加到数据库的简单应用程序。

我现在正尝试在我的活动中显示这些用户(实时更新)。

我创建了一个ListView,我相信我需要使用addChildEventListener 将数据分配给该列表视图(数组?)。我以前从来没有这样做过,所以我希望能得到一些帮助。

请在下面查看我的代码。谢谢你。

Users.kt

data class Users(
    var name: String? = "",
    var telephone: String? = ""
)



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_user_name"
        android:hint="Name"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_user_telephone"
        android:hint="Telephone"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_add_user"
        android:text="Add User"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_show_users"/>

</LinearLayout>



MainActivity.kt

package com.example.only_db.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.only_db.R
import com.example.only_db.db.Users
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var mDatabase: DatabaseReference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mDatabase = FirebaseDatabase.getInstance().reference

        bt_add_user.setOnClickListener {

            val name = et_user_name.text.toString().trim()
            val telephone = et_user_telephone.text.toString().trim()

            writeNewUser(name, telephone)
        }

        val childEventListener = object : ChildEventListener {
            override fun onCancelled(p0: DatabaseError) {
                TODO("Not yet implemented")
            }

            override fun onChildMoved(p0: DataSnapshot, p1: String?) {
                TODO("Not yet implemented")
            }

            override fun onChildChanged(p0: DataSnapshot, p1: String?) {
                TODO("Not yet implemented")
            }

            override fun onChildAdded(p0: DataSnapshot, p1: String?) {
                TODO("Not yet implemented")
            }

            override fun onChildRemoved(p0: DataSnapshot) {
                TODO("Not yet implemented")
            }

        }

        mDatabase!!.addChildEventListener(childEventListener)

    }

    private fun writeNewUser(name: String, telephone: String) {
        val user = Users(name, telephone)
        mDatabase.child("Users").push().setValue(user)
    }
}

【问题讨论】:

    标签: android firebase kotlin firebase-realtime-database


    【解决方案1】:

    为了能够显示用户,您需要听取正确的参考。因此,您将User 对象推入Users 节点,但您正在收听mDatabase,这实际上是根引用。要解决这个问题,请更改以下代码行:

    mDatabase!!.addChildEventListener(childEventListener)
    

    到:

    mDatabase.child("Users")!!.addChildEventListener(childEventListener)
    

    除此之外,在您的 onChildAdded() 方法中添加以下行以实际查看 logcat 中的结果。

    Log.d("TAG", p0.child("name").getValue(String::class.java) + "/" + p0.child("telephone").getValue(String::class.java))
    

    你会得到:

    John/123456
    Lisa/12345688
    

    如果可行,将结果映射到 List&lt;User&gt; 对象并将其传递给适配器。

    【讨论】:

    • 谢谢。让我试一试:)
    • 不客气。试一试,告诉我它是否有效。
    • 对不起,我的错。应该是Log.d("TAG", p0.child("name").getValue(String::class.java) + "/" + p0.child("telephone").getValue(String::class.java)) 现在可以了吗?
    • 您应该检查 logcat 以通过“TAG”之后的过滤器查看结果。有什么打印出来的吗?
    • 嗨,我刚刚检查了 Logcat,它正在工作。我看到的是上述格式的用户。
    猜你喜欢
    • 2022-01-19
    • 2019-02-12
    • 2021-03-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2021-07-01
    相关资源
    最近更新 更多