Unresolved Reference getResources() Kotlin

Zubair Amjad picture Zubair Amjad · Oct 19, 2018 · Viewed 10.2k times · Source

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.

Answer

sasikumar picture sasikumar · Oct 19, 2018

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())