错误 未连接适配器;跳过布局

问题描述 投票:0回答:1

当我尝试使用@GET改造从服务器获取数据时遇到问题,出现以下错误:没有连接适配器;跳过布局

主要活动

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())
                }
            }
        }
    }
}

API请求

import retrofit2.Call
import retrofit2.http.GET

interface ApiRequest {
    @GET("/api/catalog")
    fun getCatalog(): Call<List<CatalogModel>>
}

适配器

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()
    }
}

网上进行了,RecyclerView对齐方式是wrap_content,你有什么想法可能是什么问题吗?

<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>

网上进行了,RecyclerView对齐方式是wrap_content,你有什么想法可能是什么问题吗?

android kotlin retrofit2 kotlin-coroutines
1个回答
0
投票

我检查了这个,对我来说看起来不错。 我也尝试从我这边重现。它在我这边工作。 我用它作为 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" />
© www.soinside.com 2019 - 2024. All rights reserved.