Reputation: 1
I have a problem when I try to get data using @GET retrofit from the server, the following error occurs: No adapter attached; skipping layout
MainActivity
package com.example.restart
import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import com.example.restart.api.ApiRequest
import com.example.restart.api.CatalogAdapter
import com.example.restart.api.CatalogModel
import com.example.restart.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
private var adapterCatalog = CatalogAdapter()
lateinit var catalogList: ArrayList<CatalogModel>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding!!.root)
getData()
}
private lateinit var allblock: List<CatalogModel>
private fun initCategory(category: List<CatalogModel>) {
with(binding!!){
listCatalogInMain.layoutManager = GridLayoutManager(this@MainActivity, 1)
listCatalogInMain.adapter = adapterCatalog
var cataloglist: List<CatalogModel> = category
if(cataloglist.isNotEmpty()){
for(element in cataloglist){
adapterCatalog.addCatalog(element)
}
}
}
}
private fun getData(){
val api = Retrofit.Builder()
.baseUrl("https://medic.madskill.ru")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiRequest::class.java)
CoroutineScope(Dispatchers.IO).launch {
try {
val response = api.getCatalog().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
catalogList = data as ArrayList<CatalogModel>
runOnUiThread { initCategory(catalogList) }
Log.d(TAG, data.toString())
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Log.d(TAG, e.toString())
}
}
}
}
}
ApiRequest
import retrofit2.Call
import retrofit2.http.GET
interface ApiRequest {
@GET("/api/catalog")
fun getCatalog(): Call<List<CatalogModel>>
}
Adapter
package com.example.restart.api
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.restart.R
import com.example.restart.databinding.ItemBlockBinding
class CatalogAdapter: RecyclerView.Adapter<CatalogAdapter.CategoryHolder>() {
private val listCatalog = ArrayList<CatalogModel>()
class CategoryHolder(item: View): RecyclerView.ViewHolder(item){
private var bindingCategory = ItemBlockBinding.bind(item)
fun bind(category: CatalogModel){
bindingCategory.name.text = category.name
bindingCategory.category.text = category.category
bindingCategory.bio.text = category.bio
bindingCategory.price.text = category.price
bindingCategory.timeResult.text = category.time_result
bindingCategory.preparation.text = category.preparation
bindingCategory.description.text = category.description
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_block, parent, false)
return CategoryHolder(view)
}
override fun getItemCount(): Int {
return listCatalog.size
}
override fun onBindViewHolder(holder: CategoryHolder, position: Int) {
holder.bind(listCatalog[position])
}
fun addCatalog(category: CatalogModel){
listCatalog.add(category)
notifyDataSetChanged()
}
}
The Internet is conducted, the RecyclerView alignment is wrap_content, do you have any ideas what the problem might be?
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listCatalogInMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="@layout/item_block">
</androidx.recyclerview.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....
.....
</androidx.constraintlayout.widget.ConstraintLayout>
The Internet is conducted, the RecyclerView alignment is wrap_content, do you have any ideas what the problem might be?
Upvotes: 0
Views: 68
Reputation: 1
i checked this, looks fine for me. I also tried to reproduce from my side. It's working at my end. I used this as item_block.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/bio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/timeResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/preparation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="A" />
Upvotes: 0