I am new to Android Development. And I have written the code for the Recycler view for the first time. I am building the Affirmations App with the help of the Android Developer Training Program. My Code is completely the same as the stated solution code of this codelab. But I am not able to access the layout file as well as the textview in it, in my ItemAdapter.kt file. Below is the code
package com.example.affirmations.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.affirmations.model.Affirmation
/**
* Adapter for the [RecyclerView] in [MainActivity]. Displays [Affirmation] data object.
*/
class ItemAdapter(
private val context: Context,
private val dataset: List<Affirmation>
) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
// Each data item is just an Affirmation object.
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(com.example.affirmations.R.id.item_title)
}
/**
* Create new views (invoked by the layout manager)
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
// create a new view
val adapterLayout = LayoutInflater.from(parent.context)
.inflate(com.example.affirmations.R.layout.list_item, parent, false)
return ItemViewHolder(adapterLayout)
}
/**
* Replace the contents of a view (invoked by the layout manager)
*/
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = dataset[position]
holder.textView.text = context.resources.getString(item.stringResourceId)
}
/**
* Return the size of your dataset (invoked by the layout manager)
*/
override fun getItemCount() = dataset.size
}
I am facing error at line no. 26 (R.id.item_title) and 36 (R.layout.list_item)
Code of list_item.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</resources>
On running the app it gives error like this
Can't determine type for tag '<TextView android:id="@ id/item_title" android:layout_height="wrap_content" android:layout_width="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"/>'
Thank you!! in advance
CodePudding user response:
change your ItemViewHolder class and onCreateViewHolder function to
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(R.id.item_title)
}
and
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
// create a new view
val adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return ItemViewHolder(adapterLayout)
}
You should be importing your list_item from resource(R) folder.
You are using: (com.example.affirmations.R.layout.list_item, parent, false)
BUT
you should use (R.layout.list_item, parent, false)
CodePudding user response:
The Problem is Solved. I just need to create the list_item.xml file in the layout directory. I created it just randomly in the resources folder.