我正在尝试获取对我的 Firebase 数据库子项的正确引用并在我的 RecyclerView 中设置值,但我遇到了一些问题

Posted

技术标签:

【中文标题】我正在尝试获取对我的 Firebase 数据库子项的正确引用并在我的 RecyclerView 中设置值,但我遇到了一些问题【英文标题】:I am trying to get the correct reference to my Firebase Database child and set the values in my RecyclerView but I am having some issues 【发布时间】:2021-10-16 10:16:07 【问题描述】:

我正在尝试填充我的 RecyclerView ViewHolder,但我似乎遇到了困难。我认为这可能是因为数据库引用不正确,但我不太确定我做错了什么。

我尝试使用 childEventListener 中的数据快照从我的 firebase db 中检索值并传递给 RecyclerView,但不幸的是没有成功


    private var mAuth: FirebaseAuth? = null
    private var mChatKey: String? = null
    private var mDatabase: DatabaseReference? = null
    private var mMsgRef: DatabaseReference? = null

    private var mRecyclerView: RecyclerView? = null

    private var inputMsg: EditText? = null
    private var sendMsgBtn: ImageView? = null


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

        mChatKey = intent.extras!!.getString("Chat_id")

        mAuth = FirebaseAuth.getInstance()
        mDatabase = FirebaseDatabase.getInstance().reference.child("Chat").child(mChatKey!!).child("messages")


        val toolbar: Toolbar = findViewById(R.id.chat_toolbar) //make toolbar value and get xml toolbar id
        toolbar.title = "Lyrics Nigeria"
        setSupportActionBar(toolbar)
        if (supportActionBar != null) 
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
            supportActionBar!!.setDisplayShowHomeEnabled(true)
        //set back button on toolbar


        inputMsg = findViewById(R.id.chat_msg_edit)
        sendMsgBtn = findViewById(R.id.chat_send_btn)


        sendMsgBtn!!.setOnClickListener 
            saveMsgToDatabase()
            inputMsg!!.text = null
        

        mRecyclerView = findViewById(R.id.chat_recycler_view)
        mRecyclerView!!.layoutManager = LinearLayoutManager(this)
    

    override fun onStart() 
        super.onStart()
        mDatabase!!.addChildEventListener(object : ChildEventListener //set lyrics body to firebase database value

            override fun onChildAdded(dataSnapshot: DataSnapshot, p1: String?) 
                if (dataSnapshot.exists())
                    Toasty.normal(this@ChatActivity, dataSnapshot.child("message").toString()).show()
                    displayMessage()
                

            

            override fun onChildChanged(dataSnapshot: DataSnapshot, p1: String?) 
            

            override fun onChildMoved(dataSnapshot: DataSnapshot, p1: String?) 
            

            override fun onChildRemoved(dataSnapshot: DataSnapshot) 
            
            override fun onCancelled(databaseError: DatabaseError) 
            
        )
    

    private fun saveMsgToDatabase() 
        val messageKey = mDatabase!!.push().key
        val message = inputMsg!!.text.toString()
        val dateTime = SimpleDateFormat("MMM dd, yyyy HH:mm a", Locale.getDefault()).format(Date())

        val allMessageKey: HashMap<String, Any> = HashMap()
        mDatabase!!.updateChildren(allMessageKey)
        mMsgRef = mDatabase!!.child(messageKey!!)

        val msgInfoMap: HashMap<String, Any> = HashMap()
        msgInfoMap.put("name", mAuth!!.currentUser!!.displayName.toString())
        msgInfoMap.put("message", message)
        msgInfoMap.put("time", dateTime)
        mMsgRef!!.updateChildren(msgInfoMap)


    

    private fun displayMessage() 
         val mDatabaseMsgs = mDatabase!!.push().key
        val mRef = mDatabase!!.child(mDatabaseMsgs!!)
        val firebaseRecyclerAdapter = object : FirebaseRecyclerAdapter<MessageAdapter, ChatViewHolder>(MessageAdapter::class.java, R.layout.layout_listitem_inmsg_chat, ChatViewHolder::class.java, mRef) 
         override fun populateViewHolder(viewHolder: ChatViewHolder, model: MessageAdapter, position: Int) 

             viewHolder.setName(model.userName!!)
             viewHolder.setMessage(model.userMessage!!)
             viewHolder.setTime(model.msgTime!!)

         
     
     mRecyclerView!!.adapter = firebaseRecyclerAdapter
    


    class ChatViewHolder(val mView: View) : RecyclerView.ViewHolder(mView) 
        fun setName(name: String) 
            val userName = mView.findViewById<View>(R.id.user_display_text) as TextView
            userName.text = name
        

        fun setMessage(message: String) 
            val userMessage = mView.findViewById<View>(R.id.msg_display_text) as TextView
            userMessage.text = message
        

        fun setTime(time: String) 
            val msgTime = mView.findViewById<View>(R.id.time_display_text) as TextView
            msgTime.text = time
        
    

当我输入上面的代码时,应用程序崩溃了,到目前为止,我可以达到 db 值的唯一方法是使用 childEventListener 但无法填充 viewHolder。 将不胜感激任何帮助。谢谢。

以下是错误报告:

2019-09-11 17:08:42.290 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:42.347 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:42.453 10677-10677/com.lyricsnigeria.lyricsnigeria E/RecyclerView: No adapter attached; skipping layout
2019-09-11 17:08:43.283 6961-7153/? E/native: personalized-decoder.cc:832 Cannot create punctuation prediction model.
2019-09-11 17:08:43.283 6961-7153/? E/native: personalized-decoder.cc:839 Cannot create graph rescoring model.
2019-09-11 17:08:43.386 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:43.717 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.204 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.213 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.223 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.284 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.368 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:44.547 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:48.806 11188-11188/? E/ACRA: Skipping ANR Detector for process: com.instagram.android:mqtt
2019-09-11 17:08:50.051 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:50.414 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:50.635 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:50.823 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:51.465 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.501 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.523 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.613 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.656 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.696 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
2019-09-11 17:08:52.882 6961-6961/? E/RecyclerView: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.

ChatActivity xml:

<RelativeLayout 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_
    android:layout_
    android:background="@color/color4"
    android:keepScreenOn="false"
    tools:context=".ChatActivity"
    android:id="@+id/chat_recycler_lyt">


    <include
        android:id="@+id/chat_toolbar"
        layout="@layout/toolbar"
        android:layout_
        android:layout_ />

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/chat_recycler_view"
            android:layout_
            android:layout_
            android:overScrollMode="never"
            android:layout_above="@+id/input_msg_lyt"
            android:layout_below="@+id/chat_toolbar"
        tools:listitem="@layout/layout_listitem_inmsg_chat"/>

    <RelativeLayout
        android:id="@+id/input_msg_lyt"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@color/colorBackground">

        <EditText
            android:id="@+id/chat_msg_edit"
            android:layout_
            android:layout_
            android:background="@drawable/edit_text_bg2"
            android:padding="10dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:hint="@string/start_typing"
            android:singleLine="false"
            android:inputType="textCapSentences|textMultiLine"
            android:layout_toLeftOf="@+id/chat_send_btn"
            android:layout_toStartOf="@+id/chat_send_btn" />

        <ImageView
            android:id="@+id/chat_send_btn"
            android:layout_
            android:layout_
            app:srcCompat="@drawable/ic_send_black_24dp"
            android:layout_gravity="center"
            android:layout_margin="8dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

list_item xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:layout_gravity="start|bottom"
    android:layout_margin="5dp"
    android:background="@drawable/chat_bg">
        <TextView
            android:id="@+id/user_display_text"
            android:layout_
            android:layout_
            android:layout_marginStart="5dp"
            android:padding="2dp"
            android:textAllCaps="false"
            android:textColor="@color/colorBlack"
            android:textSize="12sp"
            android:layout_marginLeft="5dp" />

        <TextView
            android:id="@+id/msg_display_text"
            android:layout_
            android:layout_
            android:layout_marginStart="2dp"
            android:layout_marginLeft="2dp"
            android:padding="10dp"
            android:textAllCaps="false"
            android:textColor="@color/colorBlack"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/time_display_text"
            android:layout_
            android:layout_
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:padding="2dp"
            android:textAllCaps="false"
            android:textColor="@color/colorBlack"
            android:textSize="12sp"
            android:layout_gravity="end|bottom"/>
</LinearLayout>

适配器类:

class MessageAdapter 
    var userName:String? = null
    var userMessage:String? = null
    var msgTime:String? = null

    constructor(userName:String, userMessage:String, msgTime:String) 
        this.userName = userName
        this.userMessage = userMessage
        this.msgTime = msgTime

    
    constructor() 

还有我的数据库截图:

【问题讨论】:

分享崩溃日志你遇到了什么异常 你的数据库规则设置好了吗? 请将您的数据库截图添加到问题中 如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。请将您的数据库结构添加为 JSON 文件或至少作为屏幕截图。也请回复@AlexMamo 嗨@AlexMamo,我已按要求编辑了我的问题。 【参考方案1】:

代码中的问题在于MessageAdapter 类中的字段名称与数据库中属性的名称不同。您在 MessageAdapter 类中有三个字段,分别名为 userNameuserMessagemsgTime,而在数据库中我看到名称不同,namemessagetime,这是 正确。名称必须匹配。

一种解决方案可能是更改 MessageAdapter 类中的字段名称,如下所示:

class MessageAdapter 
    var name:String? = null
    var message:String? = null
    var time:String? = null

    constructor(name:String, message:String, time:String) 
        this.name = name
        this.message = message
        this.time = time

    
    constructor() 

您的问题将得到解决,或者您可以使用注释

class MessageAdapter 
    @get:PropertyName("name")
    @set:PropertyName("name")
    var userName:String? = null
    @get:PropertyName("message")
    @set:PropertyName("message")
    var userMessage:String? = null
    @get:PropertyName("time")
    @set:PropertyName("time")
    var msgTime:String? = null

    constructor(userName:String, userMessage:String, msgTime:String) 
        this.userName = userName
        this.userMessage = userMessage
        this.msgTime = msgTime

    
    constructor() 

你得到以下错误:

E/RecyclerView:无法滚动定位 LayoutManager 集。使用非空参数调用 setLayoutManager。

因为您试图滚动到实际上不存在的最后一个位置,因为您的RecyclerView 中没有显示任何项目。

【讨论】:

我根据您的回答更改了代码,现在出现错误:2019-09-11 21:40:01.000 23200-23200/com.lyricsnigeria.lyricsnigeria E/RecyclerView:未连接适配器;跳过布局 2019-09-11 21:40:01.220 23200-23200/com.lyricsnigeria.lyricsnigeria E/AndroidRuntime:致命异常:主进程:com.lyricsnigeria.lyricsnigeria,PID:23200 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.lyricsnigeria.lyricsnigeria.MessageAdapter 类型 这只是一个警告,通常是因为 this 而发生的。

以上是关于我正在尝试获取对我的 Firebase 数据库子项的正确引用并在我的 RecyclerView 中设置值,但我遇到了一些问题的主要内容,如果未能解决你的问题,请参考以下文章

从 Firebase 获取具有唯一 ID 的子项的值

ReactFire - 将 Firebase 绑定到 React 状态时,获取“对象作为 React 子项无效”

使用 Swift 遍历 Firebase 中的嵌套快照子项

如何在 Firebase 中显示 autoID 子项下的数据?

使用 Kotlin 的 Firebase 云消息传递 Android

如何一次获取所有 firebase 数据子项?