3

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


}
Zubair Amjad
  • 621
  • 1
  • 10
  • 29
  • you need `context` here for correct reference like `viewHolder.itemView.context.resources` `p0.itemView.context.resources` in your case – Hanzala Oct 19 '18 at 06:01
  • `[this.]getResources()` is a method that exists when you extend a class that is a `Context` for example `Activity` or has a reference internally e.g. `View`. A `RecyclerView.Adapter` does not inherit this method so you'll need a reference to some external `Context` object. Either do what @sasikumar suggests and to require an explicit reference in the constructor or use the `View` objects you are given as method arguments like above comment suggests. – zapl Oct 19 '18 at 06:34

3 Answers3

7

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())
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Okay 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 – Zubair Amjad Oct 19 '18 at 06:30
  • 1
    @ZubairAmjad `view.adapter = ForecastAdapter(weather, this)` when you do that in an `Activity`. Or `view.adapter = ForecastAdapter(weather, activity!!)` ([`getActivity()`](https://developer.android.com/reference/android/support/v4/app/Fragment.html#getActivity())) if you're doing that from a `Fragment` – zapl Oct 19 '18 at 06:41
  • When I do this though, it says it wants context, but I assume this would point to context? – Zubair Amjad Oct 19 '18 at 06:43
2

You don't need to pass it through the constructor. In the onCreateViewHolder you can take it from the parent.context

Edhar Khimich
  • 1,468
  • 1
  • 17
  • 20
-1

You can use ContextCompat.

ContextCompat.getColor(this.applicationContext, android.R.color.transparent)
YanSte
  • 10,661
  • 3
  • 57
  • 53