android - getActivity() / context in a ViewHolder in Kotlin Andorid -
i'm building viewholder , adapter fragment , when try make onclick viewholder, none of contexts pass in work. there no activity
getactivity()
can use, , p0!!.context
nor itemview.context
work either. should getting context from, , how reference it? thank you!
package com._________.criminalintent import android.os.bundle import android.support.v4.app.fragment import android.support.v7.widget.linearlayoutmanager import android.support.v7.widget.recyclerview import android.view.layoutinflater import android.view.view import android.view.viewgroup import android.widget.textview import android.widget.toast class crimelistfragment: fragment() { private var mcrimerecyclerview: recyclerview? = null private var madapter: crimeadapter? = null override fun oncreateview(inflater: layoutinflater?, container: viewgroup?, savedinstancestate: bundle?): view? { // fragment_crime_list.xml has recyclerview element = crime_recycler_view // inflate fragment activity val view = inflater!!.inflate(r.layout.fragment_crime_list, container, false) // grab recyclerview , give required layoutmanager mcrimerecyclerview = view.findviewbyid(r.id.crime_recycler_view) mcrimerecyclerview!!.layoutmanager = linearlayoutmanager(activity) updateui() return view } private fun updateui() { val crimelab = crimelab.get(activity) val crimes = crimelab.getcrimes() madapter = crimeadapter(crimes) // connect adapter recyclerview mcrimerecyclerview!!.adapter = madapter } /** * in kotlin, must give view passed constructor directly * substitute super() call * * create viewholder holders crime list item's view * * super(itemview) = super(inflater!!.inflate(r.layout.list_item_crime, parent, false)) * must give direct value in kotlin */ private class crimeholder(inflater: layoutinflater?, parent: viewgroup): recyclerview.viewholder(inflater!!.inflate(r.layout.list_item_crime, parent, false)), view.onclicklistener { private var mcrime: crime? = null /** * when given crime, crimeholder update title , date crime */ fun bind(crime: crime) { mcrime = crime val titletextview = itemview.findviewbyid<textview>(r.id.crime_title) val datetextview = itemview.findviewbyid<textview>(r.id.crime_date) titletextview.text = mcrime!!.mtitle datetextview.text = mcrime!!.mdate.tostring() } override fun onclick(p0: view?) { toast.maketext(what_to_put_here, "${mcrime!!.mtitle} clicked!", toast.length_short / 2) .show() } } private class crimeadapter(private var mcrimes: mutablelist<crime>): recyclerview.adapter<crimeholder>() { /** * - calls our crimeholder make our custom viewholders * - called recyclerview when needs new view display * - gets layoutinflater viewgroup , returns crimeholder of */ override fun oncreateviewholder(parent: viewgroup?, viewtype: int): crimeholder = crimeholder(layoutinflater.from(parent!!.context), parent) /** * bind crime (data) crimeholder */ override fun onbindviewholder(holder: crimeholder?, position: int) { holder!!.bind(mcrimes[position]) } /** * sees how many items in recyclerview need shown */ override fun getitemcount(): int = mcrimes.size } }
in implementation can safely use context
view
provided onclicklistener
override fun onclick(p0: view) { toast.maketext(p0.context, "${mcrime!!.mtitle} clicked!", toast.length_short / 2) .show() }
just remember set onclick:
fun bind(crime: crime) { mcrime = crime val titletextview = itemview.findviewbyid<textview>(r.id.crime_title) val datetextview = itemview.findviewbyid<textview>(r.id.crime_date) titletextview.text = mcrime!!.mtitle datetextview.text = mcrime!!.mdate.tostring() itemview.setonclicklistener(this) }
moreover kotlin classes nested (static
) default. private class crimeholder
equivalent private static class crimeholder
in java. that's why don't have access getactivity()
within crimeholder
Comments
Post a Comment