我需要使用recyclerView显示Firebase数据库中的数据。一切正常,我可以看到卡,但卡上没有其他所有数据。
这是我的适配器类:
class MyAdapter(c: Context, t: ArrayList<Thoughts>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
var context : Context
var theThoughts : ArrayList<Thoughts>
init{
context = c
theThoughts = t
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.card_view01, parent, false))
}//end onCreateViewHolder
override fun getItemCount(): Int {
return theThoughts.size
}//end getItemCount
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
//holder.theTopicAdapter.text[position]
//holder.theThoughtAdapter.text[position]
//it works fine withOUT these two lines up
}//end onBindViewHolder
class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
var theTopicAdapter: TextView
var theThoughtAdapter: TextView
init{
//super.itemView
theTopicAdapter = itemView.textViewCard01
theThoughtAdapter = itemView.textViewCard02
}
fun MyViewHolder(itemView: View){
super.itemView
theTopicAdapter = itemView.textViewCard01
theThoughtAdapter = itemView.textViewCard02
}//end MyViewHolder function
}//end MyViewHolder class
}//end MyAdapter class
这是我获取数据并显示它的代码:
databaseReference.addValueEventListener(object: ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()){
for (thought in p0.children){
//var theTopicGetter = thought.child("theTopic").value.toString()
val t: Thoughts = thought.getValue(Thoughts::class.java)!!
list.add(t)
}//end for loop
}//end if
adapter = MyAdapter(this@SeePreviousThoughts, list)
recyclerView.adapter = adapter
}//end onDataChange
override fun onCancelled(p0: DatabaseError) {
Toast.makeText(this@SeePreviousThoughts, "error, please try again.", Toast.LENGTH_SHORT).show()
}
})//end the listener
这是全班同学:
class Thoughts() {
lateinit var theID: String
lateinit var theCode: String
lateinit var theTopic: String
lateinit var theThought: String
constructor(theID: String, theCode: String, theTopic: String, theThought: String) : this() {
this.theID = theID
this.theCode = theCode
this.theTopic = theTopic
this.theThought = theThought
}
}
我应该从数据库中获取数据,并且应该将其显示在每张卡上,但是这些卡只是空的。 this is a screenshot of the empty recyclerView xml文件中默认包含主题和思想一词。
我想我的问题是,从未使用过类MyViewHolder中的函数MyViewHolder ... a screenshot of the code that isn't being used
答案 0 :(得分:0)
问题出在override fun onBindViewHolder(holder: MyViewHolder, position: Int)
方法中。此适配器有效:
class MyAdapter(val context: Context, val theThoughts: ArrayList<Thoughts>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.card_view01, parent, false))
}//end onCreateViewHolder
override fun getItemCount(): Int {
return theThoughts.size
}//end getItemCount
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.theTopicAdapter.text = theThoughts[position].theTopic
holder.theThoughtAdapter.text = theThoughts[position].theThought
}//end onBindViewHolder
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
lateinit var theTopicAdapter: TextView
lateinit var theThoughtAdapter: TextView
fun MyViewHolder(itemView: View){
super.itemView
theTopicAdapter = itemView.textViewCard01
theThoughtAdapter = itemView.textViewCard02
}//end MyViewHolder function
}//end MyViewHolder class
}//end MyAdapter class