ArrayAdapter.clear kotlin

Ben*_*bre 5 java android android-arrayadapter kotlin

我正在尝试学习kotlin,我想将我的一个android项目从java转换为kotlin.但我有一个问题

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误操作不支持只读集合在kotlin.collections.EmptyList.clear()与cityAdapter.clear()的行上我不知道如何解决它.

对于所有项目,请检查

有问题的WeatherFragment历史版本

当前版本

Pav*_*ngh 9

在这条线上 cityAdapter = CitySearchAdapter(context, emptyList())

emptyList 将为您提供一个不可变列表(只读),因此您需要使用

cityAdapter = CitySearchAdapter(context, arrayListOf())
Run Code Online (Sandbox Code Playgroud)

要么

cityAdapter = CitySearchAdapter(context, mutableListOf<YourType>())
Run Code Online (Sandbox Code Playgroud)

Kotlin的可变收藏品

如何初始化Kotlin的MutableList以清空MutableList?