![](/img/trans.png)
[英]Display data from API to Custom Spinner using Retrofit2 Kotlin/Java
[英]how to display categories in data class with retrofit2 - Kotlin
我正在尝试使用 koltin 中的 retrofit2 显示来自 API 的数据
但是 API 显示在我调用的数据之前有 categoryId,如下所示:
[
{
"categoryId": 0,
"categoryName": "string",
"ilmFinders": [
{
"eventId": 0,
"eventName": "string",
"eventLocation": "string",
"eventPhoto": {},
"eventDescription": "string",
"eventDate": "string",
"eventLink": "string",
"isFavourite": {}
}
]
}
]
我试图在我的数据 class 中只调用 (eventName, eventPhoto, eventLink),如下所示:
data class IimfinderData(
val eventLink: String,
val eventPhoto: String,
val eventName: String
)
API接口:
interface finderService {
@GET(" ")
fun getServices() : Call<List<IimfinderData>>
companion object Factory {
fun create(): finderService {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("PRIVATE URL")
.build()
return retrofit.create(finderService::class.java)
}
}
}
适配器:
class IimfinderAdapter(var countryList: List<IimfinderData>, var activity: MainActivity): RecyclerView.Adapter<IimfinderAdapter.ViewHolder>(){
lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IimfinderAdapter.ViewHolder {
context = parent.context!!
val view = LayoutInflater.from(context).inflate(R.layout.list_item2, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(holder: IimfinderAdapter.ViewHolder, position: Int) {
holder.eventName.text = countryList[position].eventName
holder.eventLink.text = countryList[position].eventLink
Picasso.with(activity)
.load(countryList[position].eventPhoto)
.error(R.drawable.qiblacompass)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
.into(holder.eventPhoto)
}
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
val eventName: TextView = itemView.findViewById(R.id.title_tv2)
val eventLink: TextView = itemView.findViewById(R.id.tv_url2)
val eventPhoto: ImageView = itemView.findViewById(R.id.thumbnail_tv2)
}
}
在我运行应用程序后,它显示数据是 null
我需要调用“categoryId”吗?
如果是,我如何在同一数据 class 中调用 2 arrays?
适配器和我活动中的代码是正确的,因为我用另一个 API 对其进行了测试
那我在这里做错了什么?
- - - - - - - - - - - - - - - - 解决方案 - - - - - - - - - ------
请参考答案,我必须更改从适配器调用数据的方式
而不是这个:
holder.eventName.text = countryList[position].eventName
做这个:
holder.eventName.text = countryList[position].ilmFinders[position].eventName
IimfinderData
by ListeventPhoto
是 Object,不是字符串试试这个代码
Call<List<IimfinderData>>
-> Call<List<IlmFinders>>
fun getServices() : Call<List<IlmFinders>>
和
data class IlmFinders(
@SerializedName("ilmFinders")
val ilmFinders: List<IimfinderData>
)
data class IimfinderData(
@SerializedName("eventLink")
val eventLink: String,
@SerializedName("eventPhoto")
val eventPhoto: Any,
@SerializedName("eventName")
val eventName: String
)
如果您的数据 class 属性名称与 json 元素相同,则不需要@SerializedName
当您尝试在 API 响应中访问 jsonArry 时,您可以执行以下操作:
为“IimfinderData”创建数据 class,序列化为:
data class ResponseData(
@SerializedName("name")
val userName: String,
@SerializedName("age")
val userAge: Integer,
@SerializedName("date_of_birth")
val userDOB: String)
比使用这个 class 作为数据 class 中的数据列表:
data class Response(
@SerializedName("userData")
val ilmFinders: List<ResponseData>)
使用 function 访问此数据 class 为:
fun getServices() : Call<List<Response>>
如果您没有使用与响应数据相同的名称,请确保使用@SerializedName注释
希望这能解决您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.