【问题标题】:Gson Converted data not set in Recyclerview using Retrofit2使用 Retrofit2 在 Recyclerview 中未设置 Gson 转换的数据
【发布时间】:2019-08-20 12:30:00
【问题描述】:

Gson Convertable 数据未在 Recyclerview 中使用 Retrofit2 和 rxjava2 设置,然后它的 Give Error:

“kotlin.UninitializedPropertyAccessException:lateinit 属性compositeDisposable 尚未初始化”

MainActivity.kt

internal lateinit var api : APIInterface

lateinit var compositeDisposable: CompositeDisposable

internal lateinit var companyDialog : Dialog

internal lateinit var companyAdapter: CompanyAdapter

internal lateinit var data : List<Company>


fun showCompanyPopupView(){
    companyDialog.setContentView(R.layout.compny_popup_screen)

    val rvCompany : RecyclerView = companyDialog.findViewById(R.id.rvCompany)

    rvCompany.setHasFixedSize(true)
    rvCompany.layoutManager = LinearLayoutManager(this)

    fetchData()
    companyDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    companyDialog.show()
}

private fun fetchData(){

    val retrofit = APIClient.apIClient
    if (retrofit != null) {
        api = retrofit.create(APIInterface::class.java)
    }
    compositeDisposable.add(api.getCompanyData()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({ companyList-> displayData(data)
            },{
                Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show()
            })

    )

}


private fun displayData(companyList: List<Company>) {


    val adapter = CompanyAdapter(this,companyList)
    rvCompany.adapter = adapter

}

CompanyAdapter.kt

class CompanyAdapter(内部变量上下文:上下文,内部变量 companyList:列表) :RecyclerView.Adapter(){ 覆盖乐趣 onCreateViewHolder(p0: ViewGroup, p1: Int): CompanyViewHolder { TODO("未实现") //要更改创建函数的主体,请使用 File |设置 |文件模板。

    val itemView = LayoutInflater.from(p0.context).inflate(R.layout.list_view_item,p0,false)

    return CompanyViewHolder(itemView)
}

override fun getItemCount(): Int {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    return companyList?.size!!
}

override fun onBindViewHolder(p0: CompanyViewHolder, p1: Int) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

   // p0.rbButton.text = this!!.companyList?.get(p1)?.Cmp_Name
    p0.bindModel(companyList[p1])
}

inner class CompanyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

    val radioButton : RadioButton = itemView.findViewById(R.id.rbCompanyName)

    fun bindModel(company: Company){

        radioButton.text = company.Cmp_Name
    }
}

}

【问题讨论】:

    标签: kotlin android-recyclerview retrofit2


    【解决方案1】:

    错误描述得很好,你在初始化之前使用了你的变量compositeDisposable。

    在你调用它之前用这种方式初始化你的变量:

    internal lateinit var api : APIInterface
    
    var compositeDisposable = CompositeDisposable()
    
    internal lateinit var companyDialog : Dialog
    internal lateinit var companyAdapter: CompanyAdapter
    internal lateinit var data : List<Company>
    
    fun showCompanyPopupView(){
     companyDialog.setContentView(R.layout.compny_popup_screen)
    
     val rvCompany : RecyclerView = companyDialog.findViewById(R.id.rvCompany)
    
     rvCompany.setHasFixedSize(true)
     rvCompany.layoutManager = LinearLayoutManager(this)
    
     fetchData()
     companyDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
     companyDialog.show()
    }
    
    private fun fetchData(){
       val retrofit = APIClient.apIClient
    
       if (retrofit != null) {
        api = retrofit.create(APIInterface::class.java)
      }
    
      compositeDisposable.add(api.getCompanyData()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe ({ companyList-> displayData(data)
          },{
              Toast.makeText(applicationContext, it.message, 
              Toast.LENGTH_SHORT).show()
          })
       )
    }
    

    【讨论】:

    • 现在给出,kotlin.UninitializedPropertyAccessException: lateinit property data has not been initialized 错误
    • 这是完全相同的错误。您没有在使用变量数据之前对其进行初始化。我建议您阅读this guide about lateinit以了解如何正确使用它
    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多