Unresolved Reference getResources() Kotlin

11,263

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

Share:
11,263

Related videos on Youtube

Zubair Amjad
Author by

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, 2022

Comments

  • Zubair Amjad
    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
      Hanzala over 5 years
      you need context here for correct reference like viewHolder.itemView.context.resources p0.itemView.context.resources in your case
    • zapl
      zapl over 5 years
      [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.
  • Zubair Amjad
    Zubair Amjad over 5 years
    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
  • zapl
    zapl over 5 years
    @ZubairAmjad view.adapter = ForecastAdapter(weather, this) when you do that in an Activity. Or view.adapter = ForecastAdapter(weather, activity!!) (getActivity()) if you're doing that from a Fragment
  • Zubair Amjad
    Zubair Amjad over 5 years
    When I do this though, it says it wants context, but I assume this would point to context?