0

So I am using the OpenWeatherMap Api to create a sun spotter app, so the issue I have right now is that, I have all the icons in my drawable folder, the issue is I want to display all the icons right now I have ti set up like this: enter image description here

I am using that hard coded icon as a placeholder but I want all the other icons to show, my issue is how do I do that, the image from the api is given in string format and i don't know how I would get them from by drawable folder enter image description here

Code

// Array Adapter 
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[0]
        val getDateTime = forecast.list[p1]
        val getIcon = forecast.list[p1]
//        val icon = getIcon.weather[0]
        p0.view.textView_text_clouds.text = clouds.main
        p0.view.textView_date_time.text = getDateTime.dt_txt
//        p0.view.imageView_icon = icon.icon

    }

}

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


}
// MainActivity 
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        searchButton.setOnClickListener {
            getRequest()

        }

        view.layoutManager = LinearLayoutManager(this)

//        val drawableId: Int = getResources().getIdentifier("drawable", "drawable", getPackageName())

    }

    private fun getRequest() {
        val input = searchBar.getText().toString()

        val url = "http://api.openweathermap.org/data/2.5/forecast?zip=" + input + "&units=imperial&APPID=" + getString(R.string.OPEN_WEATHER_MAP_API_KEY)

        val request = okhttp3.Request.Builder().url(url).build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object: Callback {
            override fun onResponse(call: Call, response: okhttp3.Response) {
                val body = response?.body()?.string()
                println(body)

                val gson = GsonBuilder().create()

                val weather = gson.fromJson(body, Forecast::class.java)


                runOnUiThread {
                    view.adapter = ForecastAdapter(weather)


                }

               }

            override fun onFailure(call: Call, e: IOException) {
                println("Failed to execute")
            }

        })
    }
}
Zubair Amjad
  • 621
  • 1
  • 10
  • 29

1 Answers1

0

Use When

when("forecast.list[p1]") { "icon01d.png" -> p0.view.imageView_icon.setDrawable(R.drawable.icon01d.png)

"icon02n.png" -> p0.view.imageView_icon.setDrawable(R.drawable.icon02n.png)

else -> // Your place holder icon for imageview

}

Sachin Kasaraddi
  • 597
  • 5
  • 12