【问题标题】:Kotlin, Coroutines, Kodein and list for RecyclerViewKotlin、Coroutines、Kodein 和 RecyclerView 列表
【发布时间】:2019-07-26 12:11:46
【问题描述】:

我刚开始学习 Kotlin Android,我决定在我的第一个 Kotlin 应用程序 Coroutines 和 Kodein 库中使用。我的问题是返回房间数据库中创建的对象列表。我知道如何为 Recyclerview 设置简单列表,但我在通过 Coroutines 设置 Recyclerview 中的对象列表时遇到问题。

来自使用 Recyclerview 的活动的函数:

 private fun getCities() = launch(Dispatchers.Main) {
    var locationList = addLocationViewModel.location.await()
    locationList.observe(this@AddLocationActivity, Observer {

        viewAdapter = LocationAdapter(locationList, this)
    })
}

正如您所见,LocationAdapter 中的第一个参数是 locationList,这个参数带有红色下划线。错误的意思是:

Type mismatch.
 Required: kotlin.collections.ArrayList<Location>
 Found: LiveData<List<Location>>

如何解决?

Adapter:

class LocationAdapter(
private val cities: ArrayList<Location>,
private val context: Context
) : RecyclerView.Adapter<LocationAdapter.LocationViewHolder>() {

private var removedPosition: Int = 0
private var removedItem: String = ""


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocationViewHolder {
    val layoutInflater = LayoutInflater.from(context)
    val contactRow = layoutInflater.inflate(R.layout.location_recyclerview_item, parent, false)
    return LocationViewHolder(contactRow)
}

override fun getItemCount(): Int {
    return cities.size
}

override fun onBindViewHolder(holder: LocationViewHolder, position: Int) {
    val location = holder.view.location_text_view_item

    location.text = cities.get(position).toString()
}

class LocationViewHolder(val view: View) : RecyclerView.ViewHolder(view)

fun removeItem(viewHolder: RecyclerView.ViewHolder) {
    removedItem = viewHolder.adapterPosition.toString()
    removedItem = cities[viewHolder.adapterPosition].toString()

    cities.removeAt(viewHolder.adapterPosition)
    notifyItemRemoved(viewHolder.adapterPosition)


}


}

视图模型:

class AddLocationViewModel(
private val locationRepository: LocationRepository
) : ViewModel() {

val location by lazyDeferred {
    locationRepository.getAllLocations()
}

fun insertLocation(location: Location) {
    locationRepository
}
}

【问题讨论】:

  • 您能发布您调用的视图模型方法吗?
  • 是的,我刚刚做到了。
  • 这只是你通过的位置列表,使其成为数组列表类型。看起来像它的列表
  • @RohitLalwani 你是说 ViewModel 还是 Dao 中的 Arraylist?
  • ViewModel 中的数组列表

标签: android kotlin kotlin-coroutines kodein


【解决方案1】:

错误本身不言自明,您的 VM 正在返回 LiveData&lt;List&lt;Location&gt;&gt; 类型的对象,

不要将 locationList(即 livedata)对象直接传递给适配器,而是创建如下所示的 lambda

private fun getCities() = launch(Dispatchers.Main) {
    var locationList = addLocationViewModel.location.await()
    locationList.observe(this@AddLocationActivity, Observer { 
        locList -> viewAdapter = LocationAdapter(locList, this)
    })
}

【讨论】:

  • 那么在我的情况下应该是什么样子?抱歉这个问题,我是 Kotlin 的新手,我想以正确的方式学习这个。
  • @beginner992 尝试更新的答案,如果它适合你
  • 您的解决方案有效!我还添加到函数中,带有回收器视图初始化的行,现在很好。非常感谢。
【解决方案2】:

只需获取 LiveData 对象的值,如下所示:

viewAdapter = LocationAdapter(locationList.value!!, this)

【讨论】:

  • 你的回答似乎是对的,但我对 Recyclerview 初始化有问题。当我要使用 Recyylerview 进行活动时,应用程序崩溃,我看到:lateinit property viewAdapter has not been initialized。我正在努力寻找答案,但仍然一无所获。
猜你喜欢
  • 1970-01-01
  • 2019-11-28
  • 2019-04-24
  • 2020-10-15
  • 2019-10-22
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 2020-07-10
相关资源
最近更新 更多