代码从Java转换为Kotlin - 无法正常工作

时间:2018-05-07 19:31:13

标签: java kotlin

下面的代码是从Java转换而来的,其中一些代码没有转换过来。我试图让这一切都转变为Kotlin。

    override fun onBindViewHolder(holder: PersonViewHolder, position: Int) {
        holder.mImage.setImageResource(mPersonList[position].personImage)
        holder.mTitle.text = mPersonList[position].personName
        holder.mCardView.setOnClickListener {

//Unresolved reference:Java
            val intent = intent(this, DetailActivity::class.java)
//Only safe (?.) or non-null asserted(!!.) calls are allowed on a nullable receiver of type intent?               
intent.putExtra("Title", mPersonList[holder.adapterPosition].personName)
                intent.putExtra("Description", mPersonList[holder.adapterPosition].personDescription)
                intent.putExtra("Image", mPersonList[holder.adapterPosition].personImage)
                mContext.startActivity(intent)
            }
        }

1 个答案:

答案 0 :(得分:0)

使用此代码

 holder.mCardView.setOnClickListener {
     val intent = Intent(this, DetailActivity::class.java)              
     intent.putExtra("Title", mPersonList[holder.adapterPosition].personName)
     intent.putExtra("Description", mPersonList[holder.adapterPosition].personDescription)
     intent.putExtra("Image", mPersonList[holder.adapterPosition].personImage)
     mContext?.startActivity(intent)
        }