正如标题所说,我从昨天开始使用 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)
}
}
}
这是我的活动代码:
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?) {
}
})
}
答案 0 :(得分:3)
Recyclerview未显示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
}
答案 1 :(得分:1)
我希望您的adater定义getItemCount()方法,然后您可以更改以下代码...
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 question = dataSnapshot.getValue(Question::class.java)
questions.add(question)
val adapter = QuestionAdapter(questions)
mRecyclerView.adapter = adapter
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError?) {
}
})
}
我希望适配器有以下方法......
@Override
public int getItemCount() {
return items.size();
}