代码之家  ›  专栏  ›  技术社区  ›  Keval Gangani

如何在Kotlin中初始化数组?

  •  2
  • Keval Gangani  · 技术社区  · 7 年前

    我有一个来自RESTAPI的JSON字符串,我将其绑定到 List<CategoryDO> 对象我已将所有类别/子类别数据放入此列表对象 列表(<);类别do> 但我不知道如何将这些数据中的子类别划分为 Array<List<CategoryDO>> 总体安排

    如何将子类别列表添加到 阵列(<列表(<);类别do>&燃气轮机; 对象如何声明和初始化 阵列(<列表(<);类别do>&燃气轮机; 在科特林?

    所有类别应在 列表(<);类别do> 中的格式和所有子类别 阵列(<列表(<);类别do>&燃气轮机; 总体安排

    例如:

    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>>
    

    类别数据类:

    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

    类别适配器 类别示例:

    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
        }
      .
      .
      .
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Todd    7 年前

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

    // Given
    val categories: List<CategoryDO> = TODO()
    
    val allSubCats: Array<List<CategoryDO>> = 
        categories.map { it. subCategories }.toTypedArray()
    
        2
  •  0
  •   Mobile Team ADR-Flutter    7 年前

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

    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)
        }
    }
    

    }