Unresolved Reference getResources() Kotlin
Solution 1
Pass context as parameter in your adapter then use like this
class ForecastAdapter(val forecast: Forecast,val context:Context)
then
override fun onBindViewHolder(p0: ForecastData, p1: Int) {
val drawableId: Int = context.getResources().getIdentifier("my_drawable", "drawable", getPackageName())
Solution 2
You don't need to pass it through the constructor. In the onCreateViewHolder you can take it from the parent.context
Related videos on Youtube
Zubair Amjad
I seriously suck at about me's so please bear with me! I am masters student at the University of Washington at the iSchool. My degree specializes in Information Management. I also started my own Freelance business where I work with multiple different clients where I help my clients with SEO, Website Development and provide consulting. If any of you have a project you want to pursue or want to talk about an opportunity please email me at: [email protected]
Updated on June 04, 2022Comments
-
Zubair Amjad almost 2 years
So I am trying to access the getResources() in my code, but for some reason it keeps getting an unresolved reference. I am doing this in another class for my array adapter. Is it because I don't have a main method or is it because of other reasons? I am bit new to android dev and kotlin in general so any help would be appreciate it.
class ForecastAdapter(val forecast: Forecast) : RecyclerView.Adapter<ForecastData>(){ override fun getItemCount(): Int { return forecast.list.count() } override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ForecastData { val layoutInflator = LayoutInflater.from(p0?.context) val cellForRow = layoutInflator.inflate(R.layout.weather_row, p0, false) return ForecastData(cellForRow) } override fun onBindViewHolder(p0: ForecastData, p1: Int) { val drawableId: Int = getResources().getIdentifier("my_drawable", "drawable", getPackageName()) val getWeather = forecast.list[p1] val clouds = getWeather.weather[0] val getDateTime = forecast.list[p1] var getIcon = forecast.list[p1] var icon = getIcon.weather[0] p0.view.textView_text_clouds.text = clouds.main p0.view.textView_date_time.text = getDateTime.dt_txt } } class ForecastData(val view: View): RecyclerView.ViewHolder(view){ }
-
Hanzala over 5 yearsyou need
context
here for correct reference likeviewHolder.itemView.context.resources
p0.itemView.context.resources
in your case -
zapl over 5 years
[this.]getResources()
is a method that exists when you extend a class that is aContext
for exampleActivity
or has a reference internally e.g.View
. ARecyclerView.Adapter
does not inherit this method so you'll need a reference to some externalContext
object. Either do what @sasikumar suggests and to require an explicit reference in the constructor or use theView
objects you are given as method arguments like above comment suggests.
-
-
Zubair Amjad over 5 yearsOkay and then I would pass it in like this when calling the class?
view.adapter = ForecastAdapter(weather, context)
When I do pass it in like that it says unresolved reference -
zapl over 5 years@ZubairAmjad
view.adapter = ForecastAdapter(weather, this)
when you do that in anActivity
. Orview.adapter = ForecastAdapter(weather, activity!!)
(getActivity()
) if you're doing that from aFragment
-
Zubair Amjad over 5 yearsWhen I do this though, it says it wants context, but I assume this would point to context?