kotlin 我无法在recyclerview中查看来自API的数据

kgsdhlau  于 2023-01-26  发布在  Kotlin
关注(0)|答案(2)|浏览(145)

我无法在recyclerview中查看api的数据。你能告诉我是什么问题吗?
我的代码:
适配器:

class NewsAdapter(private val data: List<AllData>) :
    RecyclerView.Adapter<NewsAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val title = view.findViewById<TextView>(R.id.news_info)
            val imageView = view.findViewById<ImageView>(R.id.news_img)
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val news:AllData=data[position]
        holder.title.text = news.haberler[position].title
        Glide.with(holder.imageView.context).load(news.haberler[position].imageUrl).into(holder.imageView)
    }

    override fun getItemCount(): Int {
        return data.size
    }

}

activity_main.xml

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/news_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view" />

主要活动:

class MainActivity : AppCompatActivity() {

    private  val itemList = ArrayList<MenuCardModel>()
    private lateinit var menuCardAdapter: MenuCardAdapter
    
    private lateinit var newsAdapter: NewsAdapter

    val data = ArrayList<AllData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadNews()
    }

    private fun loadNews() {

            val retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
    
            val service = retrofit.create(ApiInterface::class.java)
            val call = service.getNews()

        call.enqueue(object :Callback<AllData>{
            override fun onResponse(
                call: Call<AllData>,
                response: Response<AllData>
            ) {
                   response.body()?.let {
                       data.add(it)
                           newsAdapter = NewsAdapter(data)
                           news_recyclerView.adapter = newsAdapter

                   }
            }

            override fun onFailure(call: Call<AllData>, t: Throwable) {
                t.printStackTrace()
            }

        })

    }

}

型号:

data class Haberler(
    val content: String,
    val id: Int,
    val imageUrl: String,
    val orderNo: Int,
    val parentId: Any,
    val title: String,
    val videoUrl: String
)

所有数据:

data class AllData(
    val haberler: List<Haberler>,
    val istatistik: Istatistik,
    val konferans: Konferans,
    val kvkk: String,
    val liveChat: String,
    val pratikBilgiler: List<PratikBilgiler>,
    val sgkTv: List<SgkTv>,
    val sss: List<Ss>,
    val state: Boolean,
    val subjects: List<Subject>
)

你好,我无法在循环视图中查看api数据。你能帮我解决这个问题吗?
你好,我无法在循环视图中查看api数据。你能帮我解决这个问题吗?
你好,我无法在循环视图中查看api数据。你能帮我解决这个问题吗?

vsdwdz23

vsdwdz231#

看起来您忘记在设置适配器之前将布局管理器设置为recyclerview
设置布局管理器的方法有两种
第一种方式通过xml

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    ...
    />

第二种方式是通过代码

newsAdapter = NewsAdapter(data)
news_recyclerView.layoutManager = LinearLayoutManager(context) // line which you forgot
news_recyclerView.adapter = newsAdapter

更多关于布局管理器的理解,你可以参考下面的链接
https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager

ddrv8njm

ddrv8njm2#

只需在RecyclerView中添加layoutManager此行,如下所示:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    ...
    />

回收器视图缺少布局管理器

相关问题