Alejandro Diaz
Alejandro Diaz

Reputation: 41

Stuck with ArrayAdapter

I'm learning to code Kotlin as a hobby, but I've hit a wall. I'm getting the following error:

None of the following functions can be called with the arguments supplied: 
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: Array<(out) TypeVariable(T)!>) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: (Mutable)List<TypeVariable(T)!>) defined in android.widget.ArrayAdapter

It was working fine until I added the API from openweather to pull some data. Any help appreciated!

Here is my code:

package com.example.myweatherapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ForecastActivity : AppCompatActivity() {

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



         var retriever = WeatherRetriever()

            val callback = object : Callback<Weather> {
                override fun onFailure(call: Call<Weather>?, t: Throwable) {
                println("It failed")
                }

                override fun onResponse(
                    call: Call<Weather>?, response: Response<Weather>?) {
                    println("It wORKED")

                    println(response?.body()?.main)

                    title = response?.body()?.name

                    var forecasts = response?.body()?.main

                    var castListView = findViewById<ListView>(R.id.forecastListView)

                    var adapter = ArrayAdapter(this@ForecastActivity,android.R.layout.simple_list_item_1,forecasts)

                    castListView.adapter=adapter
                }

            }
            retriever.getForecast(callback)
        }

        }

Upvotes: 2

Views: 644

Answers (1)

Naresh NK
Naresh NK

Reputation: 1066

ArrayAdapter(this@ForecastActivity,android.R.layout.simple_list_item_1,forecasts)

ArrayAdapter requires Array/List of Object as the third argument but forecasts is just the response body.

See more details here about ArrayAdapter constructors

You need to parse the response and prepare a List/Array of the Objects (i.e. String titles) to pass as the 3rd argument in the ArrayAdapter constructor.

Upvotes: 1

Related Questions