How to show nested part of a json in Kotlin

Asked

Viewed 38 times

1

My class date

 data class News (
   val title: String,
   val pubDate : String,
   val link : String,
   val description : String,
   val content : String,
   val categoria : String,
   val linkImg : String
 )

My Adapter

 class NewsAdapter(private val news: List<News>,private val mCtx: Context): 
 RecyclerView.Adapter<NewsAdapter.ViewHolder>() {
 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
 ViewHolder {
    val view =LayoutInflater.from(parent.context).inflate(R.layout.news_row,parent,false)
     return ViewHolder(view)
 }

override fun getItemCount()= news.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val new = news[position]
    holder.title.text = new.title
    holder.pubDate.text = new.pubDate
    holder.description.text = new.description
    Glide.with(mCtx)
        .load(new.linkImg)
        .into(holder.img)
  }

  class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val title: TextView = itemView.textView
    val pubDate: TextView = itemView.pubDate
    val description: TextView = itemView.description
    val img: ImageView = itemView.findViewById(R.id.imageView)
  }

XML

 <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:contentDescription="TODO" />

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
    android:id="@+id/pubDate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

Json structure

{
  "title": "",
  "pubDate": "",
  "link": "",
  "guid": "",
  "author": "",
  "description": "",
  "content": "",
  "enclosure": {
    "link": "",
    "type": ""
  },
  "categories": [
    ""
  ]
}

I’m being able to access the title and so on but how can I access the link inside Enclosure?? Thank you.

  • How you turn json into your News object?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.