首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Kotlin中初始化Array<List<Model>>?

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

Stack Overflow用户
提问于 2018-05-11 12:31:36
回答 2查看 6.5K关注 0票数 2

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

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

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

例如:

代码语言:javascript
复制
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>>

CategoryDO数据类:

代码语言:javascript
复制
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)

实际上,我需要将这个单独的类别/子类别的内容传递给CategoryAdapter类。

CategoryAdapter类示例:

代码语言:javascript
复制
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
    }
  .
  .
  .
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-11 12:38:15

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

代码语言:javascript
复制
// Given
val categories: List<CategoryDO> = TODO()

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

Stack Overflow用户

发布于 2018-05-11 12:34:54

我尝试将json数据放入回收器视图适配器中,如果问题得到解决,它就能正常工作。

代码语言:javascript
复制
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)
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50292416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档