【问题标题】:No data is fetched using MVVM and Retrofit没有使用 MVVM 和 Retrofit 获取数据
【发布时间】:2020-03-19 20:13:33
【问题描述】:

我正在尝试在我的应用程序上使用MVVM 来使用Retrofit 获取一些数据,并将其显示在RecyclerView 上,但是没有显示任何内容,正在记录数据并且当我调用改造实例时直接来自我的活动,数据正在我的RecyclerView 上显示,不知道做错了什么。

这是我的适配器的样子

    class CategoryAdapter (val context: Context):RecyclerView.Adapter<CategoryAdapter.CategoryView>(){

    var category : List<Category> = listOf()

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

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


    override fun onBindViewHolder(holder:CategoryView, position: Int) {
        val category = category[position]

        holder.categoryName.text = category.name

    }


    class CategoryView(itemView: View, var category: Category? = null) : RecyclerView.ViewHolder(itemView){
        val categoryName: TextView = itemView.findViewById(R.id.category_title)

    }

}

我的ViewModel

fun loadCategories(id:Int): MutableLiveData<List<Category>>? {
    var categoryList: MutableLiveData<List<Category>>? = null

    RetrofitClient.makeRetrofitApi2().getCategoryProducts(id)
            .enqueue(object : Callback<List<Category>> {
                override fun onResponse(call: Call<List<Category>>, response: Response<List<Category>>) {
                    categoryList?.value = response.body()
                }

                override fun onFailure(call: Call<List<Category>>, t: Throwable) {

                }
            })

    return categoryList
    }

我在我的活动中如何称呼它

private fun productRecyclerView(){

    recyclerView = findViewById(R.id.foodRv)
    categoryAdapter = CategoryAdapter(this)
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.adapter = categoryAdapter


    userViewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
    userViewModel!!.loadCategories(intent.getIntExtra("VENDOR_ID", 0))?.observe(this, Observer { categories ->

        categoryAdapter = CategoryAdapter(this)
        categoryAdapter!!.setCategories(categories)
        foodRv!!.adapter = categoryAdapter

    })
}

一切都已初始化。

【问题讨论】:

  • 你没有在loadCategories()函数中初始化categoryList,你将它设置为null但从未赋值
  • API调用是否成功?

标签: android kotlin mvvm retrofit2


【解决方案1】:

您在Observer 中再次初始化了Adapter,并将其设置为foodRv。是故意的吗?

如果您需要recyclerView 来显示您的类别,您可以这样做:

categoryAdapter!!.setCategories(categories)
categoryAdapter!!.notifyDataSetChanged()

【讨论】:

    【解决方案2】:

    首先您需要在 viewModel 中初始化 categoryList,因为它的值为 null,如下所示:

     var categoryList: MutableLiveData<List<Category>>? = MutableLiveData<>()
    

    在适配器第一次使用 0 个项目时,不会出现任何内容,在将新获取的类别设置为适配器后,您需要通知它使用 DiffUtils 或简单地使用 notifyDataSetChanged() 重新膨胀项目

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2017-07-20
      • 1970-01-01
      • 2021-11-15
      • 2016-08-05
      • 2019-08-17
      • 1970-01-01
      • 2019-05-13
      • 2021-05-03
      相关资源
      最近更新 更多