我有一个来自rest的JSON字符串,我将它绑定到List<CategoryDO>对象中。我将所有的类别/子类别数据放入这个列表对象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>>CategoryDO数据类:
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类示例:
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
}
.
.
.
}发布于 2018-05-11 12:38:15
如果需要将List<CategoryDO>转换为Array<List<CategoryDO>>,其中内部列表是每个CategoryDO的子类别列表,则可以映射原始列表并将结果转换为数组.
// Given
val categories: List<CategoryDO> = TODO()
val allSubCats: Array<List<CategoryDO>> =
categories.map { it. subCategories }.toTypedArray()发布于 2018-05-11 12:34:54
我尝试将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)
}
}}
https://stackoverflow.com/questions/50292416
复制相似问题