Kotlin、Coroutines、Kodein 和 RecyclerView 列表
Kotlin, Coroutines, Kodein and list for RecyclerView
我刚开始学习 Kotlin Android,我决定在我的第一个 Kotlin 应用程序中使用协程和 Kodein 库。我的问题是返回在房间数据库中创建的对象列表。我知道如何为 Recyclerview 设置简单列表,但我在通过 Coroutines 设置 Recyclerview 中的对象列表时遇到问题。
来自 activity 的函数使用 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
}
}
错误本身不言自明,您的 VM 正在返回 LiveData<List<Location>>
、
类型的对象
与其将 locationList(即实时数据)对象直接传递给适配器,不如像下面这样创建 lambda
private fun getCities() = launch(Dispatchers.Main) {
var locationList = addLocationViewModel.location.await()
locationList.observe(this@AddLocationActivity, Observer {
locList -> viewAdapter = LocationAdapter(locList, this)
})
}
直接取LiveData对象的值,像这样:
viewAdapter = LocationAdapter(locationList.value!!, this)
我刚开始学习 Kotlin Android,我决定在我的第一个 Kotlin 应用程序中使用协程和 Kodein 库。我的问题是返回在房间数据库中创建的对象列表。我知道如何为 Recyclerview 设置简单列表,但我在通过 Coroutines 设置 Recyclerview 中的对象列表时遇到问题。
来自 activity 的函数使用 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
}
}
错误本身不言自明,您的 VM 正在返回 LiveData<List<Location>>
、
与其将 locationList(即实时数据)对象直接传递给适配器,不如像下面这样创建 lambda
private fun getCities() = launch(Dispatchers.Main) {
var locationList = addLocationViewModel.location.await()
locationList.observe(this@AddLocationActivity, Observer {
locList -> viewAdapter = LocationAdapter(locList, this)
})
}
直接取LiveData对象的值,像这样:
viewAdapter = LocationAdapter(locationList.value!!, this)