I have a problem when using data class that generated by the JSONToKotlinClass extension from Android Studio.
So i converted this JSON to data class in kotlin, The generated classes are UserDataClass
, UserDataClassItem
, Geo
, Company
and Address
.
This is what UserDataClass
looks like:
package com.example.kotlinretrofitexample
class UserDataClass : ArrayList<UserDataClassItem>()
And this is what UserDataClassItem
looks like:
package com.example.kotlinretrofitexample
import com.google.gson.annotations.SerializedName
data class UserDataClassItem(
@SerializedName("id")
var id: Int,
@SerializedName("name")
var name: String,
@SerializedName("username")
var username: String,
@SerializedName("email")
var email: String,
@SerializedName("address")
var address: Address,
@SerializedName("phone")
var phone: String,
@SerializedName("website")
var website: String,
@SerializedName("company")
var company: Company
)
So what i want to do is show these data with RecyclerView. Here's the Adapter i created:
class UserAdapter(private val data: List<UserDataClass>?) : RecyclerView.Adapter<UserAdapter.MyHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false)
return MyHolder(v)
}
override fun getItemCount(): Int = data?.size ?: 0
override fun onBindViewHolder(holder: MyHolder, position: Int) {
holder.bind(data?.get(position))
}
class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(get: UserDataClass?) {
itemView.nama.text = get?.name
itemView.email.text = get?.email
itemView.notelp.text = get?.phone
val address = "${get?.address?.street}, ${get?.address?.city}, ${get?.address?.suite}, ${get?.address?.zipcode}"
itemView.alamat.text = address
}
}
}
When Built, the error message returned:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
Unresolved reference: name
Unresolved reference: phone
Unresolved reference: address
Did i miss something?? If there are things unclear, let me know!
Edit :
This is for the Address
class:
data class Address(
@SerializedName("street")
var street: String,
@SerializedName("suite")
var suite: String,
@SerializedName("city")
var city: String,
@SerializedName("zipcode")
var zipcode: String,
@SerializedName("geo")
var geo: Geo
)
This is for the Company Class
:
data class Company(
@SerializedName("name")
var name: String,
@SerializedName("catchPhrase")
var catchPhrase: String,
@SerializedName("bs")
var bs: String
)
And this is for the Geo
class:
data class Geo(
@SerializedName("lat")
var lat: String,
@SerializedName("lng")
var lng: String
)
I believe you are getting UserDataClassItem
instead of UserDataClass
so pass the UserDataClassItem
to your adapter class in constructor.
class UserAdapter(private val data: List< UserDataClassItem >?) : RecyclerView.Adapter<UserAdapter.MyHolder>(){
//change accordingly
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.