Search code examples
androidkotlinandroid-arrayadaptercustom-arrayadapter

Unresolved Reference getResources() Kotlin


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


}


Solution

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