Я получаю данные от json: https://raw.githubusercontent.com/rolling-scopes-school/rs.android.task.6/master/data/data.json
Запрос интерфейса API:
interface ApiInterface {
@GET
fun getItems(@Url url: String): Call<GitResponse>
}
Модель:
data class GitResponse(val channel: Channel)
data class Channel(val item: List<Item>)
Адаптер:
class MyAdapter(val items: List<Item>): RecyclerView.Adapter<ItemsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemsViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_layout, parent, false)
return ItemsViewHolder(view)
}
override fun getItemCount(): Int {
return items.size
System.out.println("items.size - " + items.size)
}
override fun onBindViewHolder(holder: ItemsViewHolder, position: Int) {
val title = items[position].title ?: ""
val description = items[position].description ?: ""
val imageurl = items[position].image.url ?: ""
holder.bind(title, description, imageurl)
}
}
class ItemsViewHolder(itemView : View): RecyclerView.ViewHolder(itemView){
private val photo: ImageView = itemView.findViewById(R.id.image_photo)
private val title_text: TextView = itemView.findViewById(R.id.title_)
private val description_text:TextView = itemView.findViewById(R.id.description)
fun bind(title: String,description:String,imageurl:String) {
Glide.with(itemView.context).load(imageurl).into(photo)
title_text.text = title
description_text.text = description
}
}
В MainActivity я завершил:
val url = "https://raw.githubusercontent.com/rolling-scopes-
school/rs.android.task.6/master/data/data.json"
val request = ServiceBuilder.buildService(ApiInterface::class.java)
val call = request.getItems(url)
call.enqueue(object : Callback<GitResponse> {
override fun onFailure(call: Call<GitResponse>, t: Throwable) {
Toast.makeText(this@MainActivity, "${t.message}", Toast.LENGTH_LONG).show()
}
override fun onResponse(call: Call<GitResponse>, response: Response<GitResponse>) {
if (response.isSuccessful){
recyclerview.apply {
progress_bar.visibility = View.GONE
setHasFixedSize(true)
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = MyAdapter(response.body()?.channel?.item ?: listOf())
recyclerview.adapter = adapter
adapter.notifyDataSetChanged()
}
}
}
})
В итоге я получаю только одну (первую запись) списка. Зачем? Что не так?