如何在 Kotlin 中初始化 Array<List<Model>>?

How to initialize Array<List<Model>> in Kotlin?

我有一个来自 rest API 的 JSON 字符串,我将其绑定到 List<CategoryDO> 对象中。我将所有 category/sub-category 数据都放入此列表对象 List<CategoryDO> 中,但我不知道如何将子类别从这些数据中分离为 Array<List<CategoryDO>> 格式。

如何将子类别列表添加到 Array<List<CategoryDO>> 对象中?我如何在 Kotlin 中声明和初始化 Array<List<CategoryDO>>

所有类别应采用 List<CategoryDO> 格式,所有子类别应采用 Array<List<CategoryDO>> 格式。

例如:

List<CategoryDO of Cat-1, CategoryDO of cat-2, ... etc>

Array<List<CategoryDO of SubCat-1 of Cat-1, CategoryDO of SubCat-2 of Cat-1>>, List<CategoryDO of SubCat-12 of Cat-2, CategoryDO of SubCat-22 of Cat-2>>, ...etc>>

类别DO数据class:

data class CategoryDO(  @SerializedName("Id")
                    @Expose
                    var id: Long? = null,
                    @SerializedName("Name")
                    @Expose
                    var name: String? = null,
                    @SerializedName("SubCategories")
                    @Expose
                    var subCategories: List<CategoryDO>? = null)

实际上,我需要将这个单独的 Category/Sub-Category 东西传递给 CategoryAdapter class。

CategoryAdapter class 样本:

class CategoryAdapter : BaseExpandableListAdapter {
private var groupItem: List<CategoryDO>
private var contentItem: Array<List<CategoryDO>>
private var context: Context
private var imageOnClickListener: View.OnClickListener

constructor(context: Context, groupItem: List<CategoryDO>, contentItem: Array<List<CategoryDO>>, imageOnClickListener: View.OnClickListener) {
        this.groupItem = groupItem
        this.contentItem = contentItem
        this.context = context
        this.imageOnClickListener = imageOnClickListener
    }
  .
  .
  .
}

我尝试 json 将数据放入回收器视图适配器,它正在工作,如果解决了您的问题,您可以尝试..

class HeroAdapter(val item: MutableList<Hero>, val context: Context, val itemClick:OnRecyclerViewItemClickListener) : RecyclerView.Adapter<HeroAdapter.ItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
    val v = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_layout, parent, false)
    return ItemViewHolder(v)

}

var onClickListener: OnRecyclerViewItemClickListener? = null

open interface OnRecyclerViewItemClickListener {
    fun click(hero: Hero)
}

override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
    var hero: Hero = item[position]
    onClickListener=itemClick
    holder?.mTvName?.text = hero.getName()
    holder?.mTvBio?.text = hero.getBio()
    holder?.mTvReal?.text = hero.getRealname()
    holder?.mTvFirst?.text = hero.getFirstappearance()
    holder?.mTvTeam?.text = hero.getTeam()
    holder?.mTvPublisher?.text = hero.getPublisher()
    holder?.mTvCreate?.text = hero.getCreatedby()
    Glide.with(context)
            .load(hero.getImageurl())
            .into(holder?.mIvImage)
    holder?.itemView?.setOnClickListener(View.OnClickListener {
        this.onClickListener?.click(hero)
    })
}

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

class ItemViewHolder : RecyclerView.ViewHolder {
    var mTvName: TextView? = null
    var mTvReal: TextView? = null
    var mTvCreate: TextView? = null
    var mTvBio: TextView? = null
    var mTvTeam: TextView? = null
    var mTvPublisher: TextView? = null
    var mTvFirst: TextView? = null
    var mIvImage: ImageView? = null

    constructor(itemView: View) : super(itemView) {
        mTvName = itemView.findViewById(R.id.alTvName)
        mTvReal = itemView.findViewById(R.id.alTvRealName)
        mTvFirst = itemView.findViewById(R.id.alTvFirst)
        mTvCreate = itemView.findViewById(R.id.alTvcreatedby)
        mTvBio = itemView.findViewById(R.id.alTvBio)
        mTvTeam = itemView.findViewById(R.id.alTvTeam)
        mTvPublisher = itemView.findViewById(R.id.alTvpublisher)
        mIvImage = itemView.findViewById(R.id.alIvUserImage)
    }
}

}

如果您需要将 List<CategoryDO> 转换为 Array<List<CategoryDO>>,其中内部列表是每个 CategoryDO 的子类别列表,您可以映射原始列表并转换结果到数组...

// Given
val categories: List<CategoryDO> = TODO()

val allSubCats: Array<List<CategoryDO>> = 
    categories.map { it. subCategories }.toTypedArray()