How to display deserialized JSON data in a RecyclerView using Kotlin?

I am currently working on a project that requires a search feature that retrieves JSON data from a website. The app is supposed to display that data using a RecyclerView. However, Android studio refuses to accept my dataset into the recyclerview adapter. My deserialized JSON data is coming in the form of a List and is deserialized using the KotlinX serializer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Serializable
data class searchResults(@SerialName("name") val name: String,
@SerialName("description") val description: String,
@SerialName("website") val website: String )
val query = Query(searchBox.getText().toString())
val result = index.search(query)
val searchObject: List<searchResults> =
result.hits.deserialize(searchResults.serializer())
adapter = resultsAdapter(searchObject)
recyclerView.adapter = adapter
</code>
<code>@Serializable data class searchResults(@SerialName("name") val name: String, @SerialName("description") val description: String, @SerialName("website") val website: String ) val query = Query(searchBox.getText().toString()) val result = index.search(query) val searchObject: List<searchResults> = result.hits.deserialize(searchResults.serializer()) adapter = resultsAdapter(searchObject) recyclerView.adapter = adapter </code>
@Serializable
data class searchResults(@SerialName("name") val name: String,
                     @SerialName("description") val description: String,
                     @SerialName("website") val website: String )


val query = Query(searchBox.getText().toString())
            val result = index.search(query)    

val searchObject: List<searchResults> = 
            result.hits.deserialize(searchResults.serializer()) 

adapter = resultsAdapter(searchObject)
recyclerView.adapter = adapter

However, the adapter insists on taking the searchResults deserializer data class with it in the space that it would normally take the ViewHolder data class.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class resultsAdapter(var searchObject: List<searchResults>) : RecyclerView.Adapter<resultsAdapter.resultsViewHolder>(){
inner class resultsViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
val name: TextView = itemView.findViewById(R.id.nameResult)
val description: TextView = itemView.findViewById(R.id.descriptionResult)
val website: TextView = itemView.findViewById(R.id.websiteResult)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): resultsViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.rowlayout, parent, false)
return resultsViewHolder(view)
}
override fun getItemCount(): Int {
return searchObject.size
}
override fun onBindViewHolder(holder: resultsViewHolder, position: Int) {
val currentItem = searchObject[position]
holder.name.text = currentItem.name
holder.description.text = currentItem.description
holder.website.text = currentItem.website
}
}
</code>
<code>class resultsAdapter(var searchObject: List<searchResults>) : RecyclerView.Adapter<resultsAdapter.resultsViewHolder>(){ inner class resultsViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){ val name: TextView = itemView.findViewById(R.id.nameResult) val description: TextView = itemView.findViewById(R.id.descriptionResult) val website: TextView = itemView.findViewById(R.id.websiteResult) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): resultsViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.rowlayout, parent, false) return resultsViewHolder(view) } override fun getItemCount(): Int { return searchObject.size } override fun onBindViewHolder(holder: resultsViewHolder, position: Int) { val currentItem = searchObject[position] holder.name.text = currentItem.name holder.description.text = currentItem.description holder.website.text = currentItem.website } } </code>
class resultsAdapter(var searchObject: List<searchResults>) : RecyclerView.Adapter<resultsAdapter.resultsViewHolder>(){

inner class resultsViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
    val name: TextView = itemView.findViewById(R.id.nameResult)
    val description: TextView = itemView.findViewById(R.id.descriptionResult)
    val website: TextView = itemView.findViewById(R.id.websiteResult)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): resultsViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.rowlayout, parent, false)
    return resultsViewHolder(view)
}

override fun getItemCount(): Int {      
    return searchObject.size

}

override fun onBindViewHolder(holder: resultsViewHolder, position: Int) {

    val currentItem = searchObject[position]
    holder.name.text = currentItem.name
    holder.description.text = currentItem.description
    holder.website.text = currentItem.website


    }

}

From reading documentation online I believe the ViewHolder data class is supposed to be initialized in the results Adapter. It should say “List ViewHolder” instead of “List searchResults” during the variable initialization, however, whenever I try to implement that change I am thrown an error saying:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Type mismatch.
Required:
List<resultsData>
Found:
List<searchResults>
</code>
<code>Type mismatch. Required: List<resultsData> Found: List<searchResults> </code>
Type mismatch.
Required:
List<resultsData>
Found:
List<searchResults>

What is the correct way to do this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật