I was learning about RecyclerViews and tried making one after watching this tutorial. Everything was going great until the very end when I got an error when connecting my adapter to my MainActivity.
The error I got in my MainActivity.kt was:
Type mismatch: inferred type is RecyclerAdapter but RecyclerView.Adapter<RecyclerView.ViewHolder>? was expected
Error is on line 22
This is my MainActivity.kt
package com.example.testing
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rvTest = findViewById<RecyclerView>(R.id.rvTest)
layoutManager = LinearLayoutManager(this)
rvTest.layoutManager = layoutManager
adapter = RecyclerAdapter()
rvTest.adapter = adapter
}
}
And this is my RecyclerAdapter
package com.example.testing
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
private var Title = arrayOf("Cosmopolitan", "Vodka Sprite")
private var Image = intArrayOf(R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_background)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.rv, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
holder.itemTitle.text = Title[position]
holder.itemImage.setImageResource(Image[position])
}
override fun getItemCount(): Int {
return Title.size
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
var itemImage: ImageView
var itemTitle: TextView
init {
itemImage = itemView.findViewById(R.id.imageView)
itemTitle = itemView.findViewById(R.id.textView)
}
}
}
CodePudding user response:
In your MainActivity.kt
private var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = null
The datatype for adapter variable is wrong Change it to something like this
private var adapter: RecyclerAdapter? = null
Also instead of making it of type nullable, you can instead change to lateinit & make it non nullable type as it would be initialised inside onCreate
private lateinit var adapter: RecyclerAdapter
CodePudding user response:
change
private var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>()
to
private var adapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
CodePudding user response:
You are extending the variable with the Extension RecyclerView.Adapter<RecyclerView.ViewHolder>()
instead of the class RecyclerAdapter
.
Replace
private var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>? = null
with
private lateinit var adapter : RecyclerViewAdapter ?= null