我想将数据类用于可包裹的其他 Activity 。但是,当我想要使用variabel类型列表时,我一直停留。我不知道如何实现,因为这是我第一次使用parcelable
这是我的POJO/模型
@Parcelize
data class HistoryTree(
val id: Int,
val landOwner: String,
val treeNumber: String,
val membershipId: String,
val speciesId: Int,
val speciesName: String,
val diameter: Double,
val areaName: String,
val woodProducts: List<HistoryProduct>) :Parcelable
@Parcelize
data class HistoryProduct(
val id: Int,
val treeId: Int,
val logStatus: Int,
val orderId: Int,
val logSequenceNumber: Int,
val logNumber: String,
val diameter: String,
val length: String,
val circumference: String,
val logType: Int,
val statusPecah: Int?,
val lubangGerek: Int?,
val pinhole: Int?,
val belimbing: Int?,
val createdBy: Int,
val deleted: Int,
val isTake: Int,
val rejectStatusPetani: Int
) : Parcelable
这是我的 Activity package com.sobi.penebangan.historylist
class HistoryListActivity : AppCompatActivity(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val vm: HistoryListViewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(HistoryListViewModel::class.java) }
private var trees = mutableListOf<HistoryTree>()
private val treeAdapter = object : RecyclerView.Adapter<HistoryItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryItemViewHolder {
val ui = HistoryItemUi()
val view = ui.createView(AnkoContext.create(parent.context, parent))
return HistoryItemViewHolder(ui, view)
}
override fun getItemCount(): Int = trees.size
override fun onBindViewHolder(holder: HistoryItemViewHolder, position: Int) {
holder.bind(trees[position]) {
val product = it.woodProducts
val activity = Intent(this@HistoryListActivity, ProductHistoryListActivity::class.java)
activity.putExtra(ProductHistoryListActivity.paramTreeNumber, it.treeNumber)
activity.putExtra(ProductHistoryListActivity.paramHistoryData, it.woodProducts)
startActivity(activity)
}
}
}
我要使用变量woodProduct时出现错误,因为类型数据是List有人可以帮助我吗?
最佳答案
在第一个 Activity 中,在将列表数据添加到意图转换时添加到ArrayList<out Parcelable>
前任:
intent.putParcelableArrayListExtra("arrayList",it.woodProducts as
ArrayList<out Parcelable>)
然后在第二个 Activity 中,同时将数据转换到ArrayList前任:
val list = intent.extras
val prodList: List<HistoryProduct> = list?.get("arrayList") as ArrayList<HistoryProduct>
关于android - 如何在Kotlin中实现List <data class>(子列表)的可拆分的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63878330/