gpt4 book ai didi

android - Recyclerview 不显示来自 Firebase Kotlin 的数据

转载 作者:行者123 更新时间:2023-11-29 15:36:15 24 4
gpt4 key购买 nike

正如标题所说,我从昨天开始就在使用 Kotlin。我想用来自 Firebase 的数据填充 RecyclerView

我尝试了一些东西,我构建了一个适配器并尝试填充 RecyclerView,我的数据将转到 Firebasebase 但未显示在 RecyclerView 中,我没有收到任何错误。

我找不到任何关于我的 Kotlin 语言问题的教程

这是我的适配器:

class QuestionAdapter(var items : ArrayList<Question>): RecyclerView.Adapter<QuestionAdapter.ViewHolder>() {

override fun getItemCount(): Int {
return items.size
}

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
var userDto = items[position]
holder?.titleTextView?.text = userDto.titlu
holder?.msgTextView?.text = userDto.uid
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent?.context)
.inflate(R.layout.conv_layout, parent, false)

return ViewHolder(itemView)
}

class ViewHolder(row: View) : RecyclerView.ViewHolder(row) {
var titleTextView: TextView? = null
var msgTextView: TextView? = null

init {
this.titleTextView = row?.findViewById<TextView>(R.id.titleTextView)
this.msgTextView = row?.findViewById<TextView>(R.id.msgTextView)
}
}
}

这是我的 Activity 代码:

 private fun populalteQuestionsList() {
val mChatDatabaseReference = FirebaseDatabase.getInstance().reference.child(Constants.USERS).child(mUserID).child(Constants.CHAT).child(Constants.QUESTION)
val query = mChatDatabaseReference.orderByChild("time")
query.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot?) {
mRecyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
mRecyclerView.setHasFixedSize(true)
val questions = ArrayList<Question>()
val adapter = QuestionAdapter(questions)
mRecyclerView.adapter = adapter
}
override fun onCancelled(error: DatabaseError?) {
}
})
}

最佳答案

Recyclerview is not showing data from Firebase Kotlin

因为您没有在 questions 中添加任何数据 ArrayList<Question>()

检查你的代码

试试这个

override fun onDataChange(dataSnapshot: DataSnapshot?) {
mRecyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
mRecyclerView.setHasFixedSize(true)
val questions = ArrayList<Question>()
val adapter = QuestionAdapter(questions)
// add data here inside your questions ArrayList<Question>()
mRecyclerView.adapter = adapter
}

关于android - Recyclerview 不显示来自 Firebase Kotlin 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com