#java #android #kotlin #android-recyclerview
Вопрос:
В моем коде нет ошибок во время выполнения, но есть вышеуказанная ошибка во время компиляции.Я использую recyclerview. Вот мой код MainActivity. kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.layoutManager= LinearLayoutManager(this)
val items=fetchData()
val adapter=NewsListAdapter(items)
recyclerView.adapter=adapter
}
fun fetchData():ArrayList<String>{
val list=ArrayList<String>()
for (i in 1 until 100){
list.add("Items $i")
}
return list
}
}
NewsListAdapter.kt
class NewsListAdapter(private val items: ArrayList<String>) : RecyclerView.Adapter<NewsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
val view =LayoutInflater.from(parent.context).inflate(R.layout.items_news,parent,false)
return NewsViewHolder(view)
}
override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
val currentitem=items[position]
holder.titletext.text = currentitem
}
override fun getItemCount(): Int {
return items.size
}
}
class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titletext: TextView =itemView.findViewById(R.id.textView)
}