ArrayAdapter.clear kotlin

Beniamin Ionut Dobre :

I am trying to learn kotlin and I want to convert one of my android projects from java to kotlin. But I have a problem

override fun onResponse(call: Call<List<CitySearch>>?, response: Response<List<CitySearch>>?) {
    if(response != null && response.isSuccessful) {
        val list = response.body()
        cityAdapter.clear()
        if(list != null && !list.isEmpty()){
            cityAdapter.addAll(list)
            listView.visibility = View.VISIBLE
            recyclerView.visibility = View.GONE
            cityName.visibility = View.GONE
        }
    }
}

I get the error Operation is not supported for read-only collection at kotlin.collections.EmptyList.clear() on the line with cityAdapter.clear() I don't know how to solve it.

For all the project please check

Problematic historic version of WeatherFragment

Current Version

Pavneet_Singh :

At this line cityAdapter = CitySearchAdapter(context, emptyList())

emptyList will gives you an immutable list (read only) so you need to use

cityAdapter = CitySearchAdapter(context, arrayListOf())

or

cityAdapter = CitySearchAdapter(context, mutableListOf<YourType>())

Mutable Collections in Kotlin

How do I initialize Kotlin's MutableList to empty MutableList?

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=465314&siteId=1