访问对象Kotlin中的元素
我试图访问对象中的各个元素,然后这些元素将显示为视图的一部分。目标是将每个元素指定给对象外部的变量 我的适配器访问对象Kotlin中的元素,kotlin,Kotlin,我试图访问对象中的各个元素,然后这些元素将显示为视图的一部分。目标是将每个元素指定给对象外部的变量 我的适配器 private class MyCustomAdapter(list: List<Object>?, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() { private val listings = list; private val totalCount = totalCou
private class MyCustomAdapter(list: List<Object>?, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
private val listings = list;
private val totalCount = totalCount;
override fun getItemCount(): Int {
if (listings != null) {
return listings.size
}
return 0
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context);
val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
return CustomViewHolder(cellForRow);
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val obj = listings?.get(position);
holder.view.textView_auction_title?.text = obj.toString()
}
}
但是,我尝试将id、title和category显示为单个元素,而不是将整个对象显示为文本。我对Kotlin很陌生,因此非常感谢您的帮助。您不应该使用
列表
如果所有数据列表对象都遵循相同的id、title、category
结构,那么您应该为该列表制作一个类模型
如果该列表中有不同类型的数据,则可以使用list
并执行类似于If(obj是IdTitleCategoryModel)view.text=obj.title
的操作,或者使用list
(对于特定于类型的模型,我永远不会推荐这种方法)如果传入的json数据始终采用相同的格式,则应在使用之前将em添加到arraylist中,并且应为这些变量提供一个类模型;
(在片段或活动中执行此操作,而不是在适配器中。)
//将传入数据添加到IncomingData的数组类型:(在片段或活动中)
//现在,您可以将该数据发送到适配器:
recyclerView.adapter = MyCustomAdapter(requireActivity(), listings, this@yourfragment_or_avtivity)
private class MyCustomAdapter(private val listFeed: ArrayList<IncomingData>, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
private val totalCount = totalCount
override fun getItemCount(): Int {
return listFeed.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context);
val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
return CustomViewHolder(cellForRow);
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val obj = listFeed.get(position)
holder.view.textView_auction_title?.text = obj.title
}
}
//之后,您可以从适配器访问数据:
recyclerView.adapter = MyCustomAdapter(requireActivity(), listings, this@yourfragment_or_avtivity)
private class MyCustomAdapter(private val listFeed: ArrayList<IncomingData>, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
private val totalCount = totalCount
override fun getItemCount(): Int {
return listFeed.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context);
val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
return CustomViewHolder(cellForRow);
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val obj = listFeed.get(position)
holder.view.textView_auction_title?.text = obj.title
}
}
private类MyCustomAdapter(private val listFeed:ArrayList,totalCount:Int):RecyclerView.Adapter(){
private val totalCount=totalCount
重写getItemCount():Int{
返回listFeed.count()
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):CustomViewHolder{
val layoutInflater=layoutInflater.from(parent.context);
val cellForRow=layoutInflater.flate(R.layout.auction_main,parent,false);
返回CustomViewHolder(cellForRow);
}
覆盖BindViewHolder(holder:CustomViewHolder,位置:Int){
val obj=listFeed.get(位置)
holder.view.textView\u auction\u title?.text=obj.title
}
}
“不同类型的数据”已经有问题,您应该避免列出不同类型的数据。
recyclerView.adapter = MyCustomAdapter(requireActivity(), listings, this@yourfragment_or_avtivity)
private class MyCustomAdapter(private val listFeed: ArrayList<IncomingData>, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
private val totalCount = totalCount
override fun getItemCount(): Int {
return listFeed.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context);
val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
return CustomViewHolder(cellForRow);
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val obj = listFeed.get(position)
holder.view.textView_auction_title?.text = obj.title
}
}