为啥我的 RecyclerView 没有填充来自改造的数据?
Posted
技术标签:
【中文标题】为啥我的 RecyclerView 没有填充来自改造的数据?【英文标题】:Why my RecyclerView is not filled with data from retrofit?为什么我的 RecyclerView 没有填充来自改造的数据? 【发布时间】:2021-08-01 04:54:00 【问题描述】:我的 recyclerview 有问题。我使用改造获得列表,我获取了所有数据,如日志所示:
D/OkHttp: "data":["user_profile_id":45,"recipe_id":1,"title":"earum","category":"dessert","tags":["vegetarian","family","fast-food","on-hot","advanced"],"photos":[],"likes":"logged_user_liked":false,"likes_count":0,"user_profile_id":23,"recipe_id":2,"title":"nisi","category":"additives","tags":["for-party","for-beginners","for-beginners","fast-food","advanced"],"photos":[],"likes":"logged_user_liked":false,"likes_count":4,"user_profile_id":31,"recipe_id":3,"title":"asperiores","category":"soup","tags":["healthy","fast-food","advanced","for-diabetics","vegetable"],"photos":[],"likes":"logged_user_liked":false,"likes_count":2,"user_profile_id":24,"recipe_id":4,"title":"vitae","category":"breakfast","tags":["caloric","healthy","caloric","with-meat","cold"],"photos":[],"likes":"logged_user_liked":false,"likes_count":0,"user_profile_id":16,"recipe_id":5,"title":"odit","category":"salads","tags":["for-party","for-party","vegetable","cold","for-diabetics"],"photos":[],"likes":"logged_user_liked":false,"likes_count":2,"user_profile_id":5,"recipe_id":6,"title":"vero","category":"drink","tags":["no-salt","family","with-meat","for-beginners","cold"],"photos":[],"likes":"logged_user_liked":false,"likes_count":2,"user_profile_id":16,"recipe_id":7,"title":"quisquam","category":"dessert","tags":["healthy","family","caloric","on-hot","on-hot"],"photos":[],"likes":"logged_user_liked":false,"likes_count":1,"user_profile_id":36,"recipe_id":8,"title":"sit","category":"snacks","tags":["little-fat","cold","little-fat","vegetarian","with-meat"],"photos":[],"likes":"logged_user_liked":false,"likes_count":0,"user_profile_id":33,"recipe_id":9,"title":"sed","category":"salads","tags":["cold","with-meat","for-party","caloric","advanced"],"photos":[],"likes":"logged_user_liked":false,"likes_count":4,"user_profile_id":44,"recipe_id":10,"title":"enim","category":"main-course","tags":["family","on-hot","healthy","cold","advanced"],"photos":[],"likes":"logged_user_liked":false,"likes_count":0,"user_profile_id":25,"recipe_id":11,"title":"soluta","category":"breakfast","tags":["with-meat","no-salt","no-salt","fast-food","vegetable"],"photos":[],"likes":"logged_user_liked":false,"likes_count":1,"user_profile_id":9,"recipe_id":12,"title":"eius","category":"main-course","tags":["family","caloric","on-hot","for-diabetics","fast-food"],"photos":[],"likes":"logged_user_liked":false,"likes_count":3,"user_profile_id":40,"recipe_id":13,"title":"neque","category":"main-course","tags":["healthy","vegetarian","for-diabetics","no-salt","for-party"],"photos":[],"likes":"logged_user_liked":false,"likes_count":3,"user_profile_id":38,"recipe_id":14,"title":"atque","category":"snacks","tags":["caloric","no-salt","cold","fast-food","healthy"],"photos":[],"likes":"logged_user_liked":false,"likes_count":0,"user_profile_id":19,"recipe_id":15,"title":"atque","category":"preserves","tags":["family","fast-food","vegetarian","vegetarian","caloric"],"photos":[],"likes":"logged_user_liked":false,"likes_count":2],"pagination":"total":50,"count":15,"per_page":15,"current_page":1,"total_pages":4
<-- END HTTP (3140-byte body)
I/System.out: something wrong happend
奇怪的是,有了数据,我也得到了 onFailure 响应,而我的 recyclerview 没有填充数据。
这是我的改造电话的代码:
RetrofitClient.instance.fetchAllRecipes().enqueue(object: Callback<List<DataRecipes>>
override fun onResponse(
call: Call<List<DataRecipes>>,
response: Response<List<DataRecipes>>
)
response.body()?.let
recipesList = it
tasksRecyclerView.layoutManager = LinearLayoutManager(requireContext())
recipesAdapter = RecipesAdapter(recipesList)
tasksRecyclerView.adapter = recipesAdapter
override fun onFailure(call: Call<List<DataRecipes>>, t: Throwable)
println("something wrong happened")
)
从适配器:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipesViewHolder
return RecipesViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.recipe_cardview,parent,false))
inner class RecipesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
val title: TextView
val category : TextView
val tags : TextView
val photo : ImageView
//
init
title = itemView.findViewById(R.id.TitleTV)
category = itemView.findViewById(R.id.Categories)
tags = itemView.findViewById(R.id.tags)
photo = itemView.findViewById(R.id.Recipe_photo)
override fun onBindViewHolder(holder: RecipesViewHolder, position: Int)
val item = listOfRecipes[position]
holder.itemView.apply
holder.title.text = item.data.title
holder.category.text = item.data.category
holder.tags.text = item.data.tags.toString()
Picasso.get()
.load(item.data.photos?.data?.url)
.into(holder.photo)
override fun getItemCount(): Int
return listOfRecipes.size
你有什么建议我做错了什么吗?
【问题讨论】:
println("something wrong happened")
这没用,这就是你苦苦挣扎的原因,你应该记录实际的异常,所以你应该记录t: Throwable
以了解为什么它失败了
【参考方案1】:
根据您的 JSON 响应,您的列表不是 DataRecipies,而是
【讨论】:
以上是关于为啥我的 RecyclerView 没有填充来自改造的数据?的主要内容,如果未能解决你的问题,请参考以下文章
Recyclerview 未显示来自 Firebase Kotlin 的数据
使用从 SQLite 概括的数据填充 RecyclerView/ListView
我想在我的 recyclerview 中显示来自 URL 的图片 [重复]