kotlin.NotImplementedError:未实现操作:未实现

时间:2019-04-01 09:12:50

标签: kotlin retrofit2 rx-java2 recycler-adapter

服务器JSON使用retrofit2rxjava2进行数据解析。进入CompanyAdapter类时,数据获取并成功存储在List中,然后给出上述错误。

MainActivity.kt

 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{displayData(it.company)
                }
        )
  }

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

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

    }

ComnyAdapter.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.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
        }
    }
}

1 个答案:

答案 0 :(得分:3)

原因很简单:用TODO执行一行时,它会抛出Not Implemented Exception。 只需从您的代码中删除所有TODO

class CompanyAdapter(internal var context: Context, internal var companyList: List<Company>)
    :RecyclerView.Adapter<CompanyAdapter.CompanyViewHolder>()
{
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CompanyViewHolder {
        val itemView = LayoutInflater.from(p0.context).inflate(R.layout.list_view_item,p0,false)

        return CompanyViewHolder(itemView)
    }

    override fun getItemCount(): Int {

        return companyList.size
    }

    override fun onBindViewHolder(p0: CompanyViewHolder, p1: Int) {
        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
        }
    }
}