0

While coding an android app in Kotlin I came across this error: enter image description here

I am using the OpenWeatherMapApi and from the API I am trying to grab the Forecast Data. And I was able to pull the Forecast data thanks to okhttp3 and Gson but now my issue is displaying that data. I created and a new class for a custom ArrayAdapter where I try to display in the textView that I created, but I am completely at a loss as to why I keep getting the error the array shouldn't have a size of 1 but it ends up having it

Code

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.weather_row.view.*

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 getWeather = forecast.list[p1]
        val  clouds = getWeather.weather[p1]
        p0.view.textView_text_clouds.text = clouds.main
    }

}

class ForecastData(val view: View): RecyclerView.ViewHolder(view){


}
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Zubair Amjad
  • 621
  • 1
  • 10
  • 29
  • 1
    The problem is likely with `getWeather.weather[p1]`. The `weather` "list" usually only has one item. You should be using `getWeather.weather[0]`. – TheWanderer Oct 18 '18 at 20:52
  • Alright that actually worked! Thank you! How do I accept this as the right answer since it is in the comments? – Zubair Amjad Oct 18 '18 at 20:58

1 Answers1

0

Your problem is with

getWeather.weather[p1]

The weather "list" isn't really a list. It usually only has one element.

Use this instead:

getWeather.weather[0]
TheWanderer
  • 16,775
  • 6
  • 49
  • 63