Kan*_*nan 5 kotlin android-recyclerview retrofit2 rx-java2
使用 Retrofit2 和 rxjava2 未在 Recyclerview 中设置 Gson Convertable 数据,然后通过其订阅给出错误:
UninitializedPropertyAccessException: lateinit property data has not been initialized
Run Code Online (Sandbox Code Playgroud)
通过retrofit2和rxjava2解析JSON数据。解析GSON数据转换GSon时,rxjava2订阅数据然后给出lateinit属性错误并且它没有在recyclerview中设置。
MainActivity.kt
class Company : AppCompatActivity() {
internal lateinit var api : APIInterface
var compositeDisposable = CompositeDisposable()
internal lateinit var companyDialog : Dialog
internal lateinit var adapter: CompanyAdapter
internal lateinit var data : List<Company>
internal lateinit var rvCompany : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_company)
companyDialog = Dialog(this)
//companyAdapter = CompanyAdapter()
btnSelectCompany.setOnClickListener{
showCompanyPopupView()
}
}
fun showCompanyPopupView(){
companyDialog.setContentView(R.layout.compny_popup_screen)
rvCompany = companyDialog.findViewById(R.id.rvCompany)
rvCompany.setHasFixedSize(true)
rvCompany.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?
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>) {
adapter = CompanyAdapter(this,companyList)
rvCompany.adapter = adapter
}
}
Run Code Online (Sandbox Code Playgroud)
公司适配器.kt
class CompanyAdapter(internal var context: Context, internal var companyList: List<Company>)
:RecyclerView.Adapter<CompanyAdapter.CompanyViewHolder>()
{
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CompanyViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
发生错误的原因是您从未初始化它 ( data = ...
),而是在 中访问它{ companyList-> displayData(data) }
。这忽略了companyList
您从中获得的subscribe
,这可能不是您真正想要的。
您的代码似乎过度使用了lateinit
很多。当你真正需要的时候使用它。
归档时间: |
|
查看次数: |
20896 次 |
最近记录: |