```class ViewHolder(itemView: View) : RecyclerView...
# android
z
Copy code
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    init {
        itemView.setOnClickListener {
            val intent = Intent(itemView.context, ListFragment::class.java)

            itemView.context.startActivity(intent)

        }
    }

}
why is it giving me an error message saying i need to declare fragment into manifest file? is it because of the intent?
a
Intents may only refer to component classes registered in your manifest. You cannot startActivity for a fragment class, only for an activity class. (And that class must be registered in the manifest)
z
hmm - is there another way to do this then?
a
A great many, yes. 🙂 But it depends on how the rest of your app is set up. If you're looking for a recommendation on how to use fragments as destination screens, look at the jetpack navigation library
You will want to use the NavigationController to navigate to the destination in your nav graph that corresponds to that fragment instead of using an intent and startActivity
z
I am using recyclerview in a fragment. that recyclerview is holding a list of API to create a view.
holding contents of API
a
Check this “Recycler View Using kotlin with click listener” by Aayush Puranik https://link.medium.com/z4jovX6An9
It's a better way to handle click on recyclerview
z
thanks! reading now
a
BTW: keep in mind that you don't need to pass the
context
to the adapter, the article is doing that, but IMHO is bad practice
z
Copy code
interface CellClickListener {
    fun onCellClickListener(data: HomeFeed)
}
so this is my interface. i need to put in the fragment. should it be in oncreateview?
Copy code
class ListFragment : Fragment() {

    interface CellClickListener {
        fun onCellClickListener(data: HomeFeed)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view: View = inflater.inflate(R.layout.fragment_list, container, false)

        return view
    }
}
@Alejandro Rios
a
created as a file apart and then put this
class ListFragment : Fragment(), CellClickListener
z
ahh okay. thank you sir
im so upset - what am i doing wrong now
i followed the isntructions
Copy code
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val currentItem = homeFeed.data[position]
    holder.itemView.counter_image.text = currentItem.bookNumber.toString()
    holder.itemView.title.text = currentItem.book[0].name
    holder.itemView.disc.text = currentItem.book[1].name
    holder.itemView.setOnClickListener {
        cellClickListener.onCellClickListener(currentItem)
    }
}
image.png
a
How's you adapter declared?
z
Copy code
class RecyclerAdapter(val homeFeed: HomeFeed) :
    RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(
            R.layout.recyclerlist, parent, false
        )

        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = homeFeed.data[position]
        holder.itemView.counter_image.text = currentItem.bookNumber.toString()
        holder.itemView.title.text = currentItem.book[0].name
        holder.itemView.disc.text = currentItem.book[1].name
        holder.itemView.setOnClickListener {
            cellClickListener.onCellClickListener(currentItem)
        }
    }

    override fun getItemCount() = homeFeed.data.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


    }

}
a
🤔
z
i think i got it
one second
image.png
ugh now what lol
a
ok: 1. adapter should receive a list of
HomeFeed
and the
CellClickListener
2. ViewHolder should be declared in another class, not in the same as the adapter. 3. ViewHolder can contain a function called
bind
which receives one homeFeed item and you display it as you wish
z
for #2, are you sure?
👌 1
if i move it out, it gives me an error
image.png
a
do you have a gist with those files?